-
-
Save zspencer/c3e8baeec900858fd053 to your computer and use it in GitHub Desktop.
Converting a vector of maps to a map of vectors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn maps->map-of-vectors | |
[maps] | |
(let [base (zipmap (mapcat keys maps) (repeat []))] | |
(apply merge-with conj base maps))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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