Skip to content

Instantly share code, notes, and snippets.

@latompa
Last active December 25, 2015 07:48
Show Gist options
  • Save latompa/6941443 to your computer and use it in GitHub Desktop.
Save latompa/6941443 to your computer and use it in GitHub Desktop.
; given {1 [:a :b :c] 2 [:b :c :d]}
; flip the keys like so
; {:a [1] :b [1 2] :c [1 2] :d [2]
(defn explode-keys-to-map
"take [a b c] 1, return {a [1] b [1] c [1]}"
[m ks v]
(let [add-val-to-seq (fn [m k v]
(if (m k)
(assoc m k (conj (m k) v))
(assoc m k [v])))]
(reduce #(add-val-to-seq %1 %2 v) m ks)))
(reduce (fn [m [k v]] (explode-keys-to-map m k v)) {}
(clojure.set/map-invert {1 [:a :b :c] 2 [:b :c :d]}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment