Idiomatically combine maps into displaying sets of values ​​with clojure

I am trying to idiomatically combine several cards into one card using clojure.

Enter

{:a 1 :b "a"}
{:a 2 :b "b"}
{:a 3 :b "c"}
{:a 4 :b "a"}

Expected

{:a #{1,2,3,4}, :b #{"a" "b" "c"}} 

The values ​​for each key are converted to a set of values ​​in the source maps.

+5
source share
7 answers

I would use merge-with, using a pre-built structure containing empty sets:

(def data [{:a 1 :b "a"}
           {:a 2 :b "b"}
           {:a 3 :b "c"}
           {:a 4 :b "a"}])

(let [base {:a #{} :b #{}}]
  (apply merge-with conj base data))

=> {:a #{1 2 3 4}, :b #{"a" "b" "c"}}

The trick of using empty sets in the base map is that it conjhas a specific object that it is working on, and therefore works correctly.

+5
source

merge-with can be used for this:

(def d [{:a 1 :b "a"}
        {:a 2 :b "b"}
        {:a 3 :b "c"}
        {:a 4 :b "a"}])

(def initial (into {} (map #(vector  %1 [])  (keys (apply merge d)))))

(into {} (map (fn [[a b]] [a (set b)]) 
           (apply merge-with (fn [a b] (conj a b)) initial  d)))
+4
source
(defn value-sets [& maps]
  (reduce (fn [acc map]
            (reduce (fn [m [k v]]
                      (update-in m [k] (fnil conj #{}) v))
                    acc
                    map))
          {} maps))

, ,

(defn value-sets [& maps]
  (reduce (fn [acc [k v]]
            (update-in acc [k] (fnil conj #{}) v))
          {}
          (apply concat maps)))

, : -:

(defn value-sets [maps]
  (apply merge-with into (for [m maps, [k v] m] 
                           {k #{v}})))
+2

reduce :

(def maps [{:a 1 :b 2}
           {:a 11 :b 22 :c 5}
           {:c 6 :a 7}])

(defn my-merge-maps
  [maps]
  (reduce (fn [accum [k v]]
            (if (accum k)
              (assoc accum k (conj (accum k) v))
              (assoc accum k #{v})))
          {}
          (apply concat maps)))

(defn -main
  []
  (println (my-merge-maps maps)))

: {:c #{5 6}, :b #{2 22}, :a #{1 7 11}}

(thanks to gfredericks for pointing out that I accidentally used varargs. :))

Edit: And here in a different way using merge-with:

(def maps [{:a 1 :b 2}
           {:a 11 :b 22 :c 5}
           {:c 6 :a 7}])

(defn build-up-set
  [curr-val new-val]
  (if (set? curr-val)
    (conj curr-val new-val)
    #{curr-val new-val}))

(defn my-merge-maps
  [maps]
  (apply merge-with build-up-set maps))

(defn -main
  []
  (println (my-merge-maps maps)))
+1
source

And here is the pipelined method:

(defn my-merge-maps [stuff]
  (->> stuff
       (apply concat)
       (group-by first)
       (map (fn [[k vs]] [k (set (map second vs))]))
       (into {})))

Is it possible to do this on maps if we had the correct basic functions for manipulating them?

0
source
(def data [{:a 1 :b "a"}
           {:a 2 :b "b"}
           {:a 3 :b "c"}
           {:a 4 :b "a"}])

(apply
  merge-with
  clojure.set/union
  (map #(zipmap (keys %) (map hash-set (vals %))) data))
;{:b #{"a" "b" "c"}, :a #{1 2 3 4}}
0
source

Amalloy yesterday rejected a decision on the same issue at the IRC:

(defn value-sets [& maps]                                                      
  (apply merge-with into (for [m maps, [k v] m] {k #{v}})))
0
source

All Articles