Last active
November 20, 2018 04:24
-
-
Save eneroth/d9c5deb5ab1947dd282c26000b73555f to your computer and use it in GitHub Desktop.
Deep merge with vector, seq and set handling
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- deep-merge | |
"Takes a collection of maps and deep merges them. I.e... | |
[{:a {:b 1}} | |
{:a {:c 2}}] | |
... will be merged into ... | |
[{:a {:b 1 | |
:c 2}}] | |
If the maps contain vectors for the same path into the map, those | |
vectors are concatenated. The behaviour for when the maps contain | |
vectors for the same path where other maps contain other values, | |
is undefined." | |
[& maps] | |
(let [f (fn [old new] | |
(condp every? [old new] | |
map? (deep-merge old new) | |
vector? (into old new) | |
seq? (concat old new) | |
set? (into old new) | |
new))] | |
(apply merge-with f maps))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment