HiveBrain v1.2.0
Get Started
← Back to all entries
snippetMinor

Create a sorted-map of coordinates

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
sortedmapcoordinatescreate

Problem

I wrote a little function to create a sorted-map with vectors of coordinates as keys and an empty map as a default value:

(defn empty-board [rows cols]
(into (sorted-map)
(for [x (range cols) y (range rows)]
[[x y] {}])))


Is there is a more idiomatic way of achieving this?

Solution

Yes, this is the right way to do it.

Many Clojure programs do not bother with creating empty maps. The absence of a value or nil behave like an empty map. For example (assoc nil :foo :bar) returns {:foo :bar}. You may be able to initialize to an empty sorted-map, and only assoc-in novelty as you need it ... depending on your logic.

Context

StackExchange Code Review Q#107093, answer score: 2

Revisions (0)

No revisions yet.