Tuesday, 15 July 2014

vector - Clojure: adding to a map -



vector - Clojure: adding to a map -

if have vector of maps

(def v [{:key1 "value 1" :key2 "value2"} {:key1 "value 3" :key2 "value4"}])

and map

(def m {:key3 "value2" :key4 "value5"})

how add together map m maps in vector v values of 2 given keys (in case key2 , key3) equal?

the expected result this:

[{:key1 "value 1" :key2 "value2" :key3 "value2" :key4 "value5"} {:key1 "value 3" :key2 "value4"}]

you want merge each map m3 if key2 , k3 equal:

(map (fn [x] (if (= (:key2 x) (:key3 m)) (merge m x) x)) v)

=>

({:key3 "value2", :key2 "value2", :key4 "value5", :key1 "value 1"} {:key2 "value4", :key1 "value 3"})

vector map clojure

No comments:

Post a Comment