Skip to content

Instantly share code, notes, and snippets.

@methylene
Created January 15, 2014 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save methylene/8439568 to your computer and use it in GitHub Desktop.
Save methylene/8439568 to your computer and use it in GitHub Desktop.
clojure invert multi-valued map
(defn invert-map [m]
(reduce-kv
(fn [acc k v]
(merge-with (comp vec concat)
acc
(apply array-map (interleave v (repeat [k])))))
{} m))
@methylene
Copy link
Author

(invert-map {:a [:foo :bar] :b [:baz] :c [:baz]})
;; => {:baz [:c :b], :bar [:a], :foo [:a]}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment