I have a list of keys: (1 2 3 4)
I want a map with the values set to 0 like this: {1 0, 2 0, 3 0, 4 0}
. How do I do that?
You could do something like this with the zipmap function:
(zipmap '(1 2 3 4) (repeat 0))
=> {4 0, 3 0, 2 0, 1 0}
zipmap takes a list of keys and a list of values and converts them into a map. The repeat
function creates an infinite sequence of 0s. zipmap
stops when it reaches the end of the shorter list, so just don't pass it two infinite sequences :)
You can also create a function with James' zipmap:
Clojure=> (defn map-to-n [n] (zipmap (range 1 n) (repeat 0)))
#'user/map-to-n
Clojure=> (map-to-n 10)
{9 0, 8 0, 7 0, 6 0, 5 0, 4 0, 3 0, 2 0, 1 0}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With