clojure - How to merge maps and get a map of lists? -


let's list of maps. maps have same keywords, don't know keywords beforehand.

[{:a 1 :b 2} {:a 3 :b 4}] 

and idiomatic way of merging list such map:

{:a [1 3]  :b [2 4]} 

doesn't seem hard, start implement function, gets super ugly , repetitive. have feeling there cleaner ways of achieving this.

thank you

you can pretty elegant solution using several functions standard library:

(defn consolidate [& ms]   (apply merge-with conj (zipmap (mapcat keys ms) (repeat [])) ms)) 

example:

(consolidate {:a 1 :b 2} {:a 3 :b 4}) ;=> {:a [1 3], :b [2 4]} 

one cool thing solution works if maps have different key sets.