leontalbot
leontalbot

Reputation: 2543

How can I merge hash-maps that are inside a collection of vectors in clojure?

Given collection of vectors of separated hash-maps

How can I go from :

[[{:a 1} {:b 2} {:c 3}] [{:a 4} {:b 5} {:c 6}] [{:a 7} {:b 8} {:c 9}]]

To:

[[{:a 1 :b 2 :c 3}] [{:a 4 :b 5 :c 6}] [{:a 7 :b 8 :c 9}]]

Thanks you for your answers!

Upvotes: 5

Views: 1272

Answers (2)

Ankur
Ankur

Reputation: 33637

(def data [[{:a 1} {:b 2} {:c 3}] [{:a 4} {:b 5} {:c 6}] [{:a 7} {:b 8} {:c 9}]])

(mapv #(-> [(into {} %)]) data)

Upvotes: 4

mtyaka
mtyaka

Reputation: 8848

(def coll [[{:a 1} {:b 2} {:c 3}] [{:a 4} {:b 5} {:c 6}] [{:a 7} {:b 8} {:c 9}]])

(mapv (fn [v] [(apply merge v)]) coll)
;; => [[{:a 1 :c 3 :b 2}] [{:a 4 :c 6 :b 5}] [{:a 7 :c 9 :b 8}]]

Upvotes: 7

Related Questions