Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@zspencer
Last active August 29, 2015 14:08
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 zspencer/c3e8baeec900858fd053 to your computer and use it in GitHub Desktop.
Save zspencer/c3e8baeec900858fd053 to your computer and use it in GitHub Desktop.
Converting a vector of maps to a map of vectors
(defn maps->map-of-vectors
[maps]
(let [base (zipmap (mapcat keys maps) (repeat []))]
(apply merge-with conj base maps)))
(deftest maps->map-of-vectors
(testing "converting a vector of maps to a map of vectors"
(is (= { :a [1,3] :b [2,4] }
(maps->map-of-vectors [{:a 1 :b 2} {:a 3 :b 4}])))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment