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?

ferada
11.4k25 silver badges65 bronze badges
asked Oct 9, 2015 at 18:57
\$\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.

ferada
11.4k25 silver badges65 bronze badges
answered Nov 3, 2015 at 22:42
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.