3
\$\begingroup\$

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?

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.