user2179977
user2179977

Reputation: 656

In clojure, how does one bind from a map?

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?

Upvotes: 2

Views: 137

Answers (2)

sw1nn
sw1nn

Reputation: 7328

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

Upvotes: 10

Mikita Belahlazau
Mikita Belahlazau

Reputation: 15419

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))

Upvotes: 2

Related Questions