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?
1 Answer 1
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.