Given the map:
(def myMap {"a" 1 "b" 2 "c" 3 "d" 4 "e" 5 "f" 6})
I wish to use let
to bind each key to each value. What is the most concise way to do this?
with myMap
as declared, destructuring of string keys seems like the best bet:
(let [{:strs [a b c d e f]} myMap]
(println a b c d e f))
;=> 1 2 3 4 5 6
You can use desctructuring:
I would suggest first to convert all keys to keywords using custom function like this:
(defn keywordize [map]
(into {}
(for [[k v] map]
[(keyword k) v])))
And then use destructuring:
(let [{:keys [a b c d e f]} (keywordize myMap)]
(println a b c d e f))
[{:keys [a1 a2 a3 a4 a5 ... a98 a99 a100]} (keywordize myMap)]
.
Commented
Apr 8, 2014 at 12:07
(vec keys myMap)
of the keywords automatically and pass that to the :keys function.
Commented
Apr 8, 2014 at 12:11
(vec (keys myMap))
, you would need a macro to do what you propose.