Skip to content

Instantly share code, notes, and snippets.

@eneroth
Last active November 20, 2018 04:24
Show Gist options
  • Save eneroth/d9c5deb5ab1947dd282c26000b73555f to your computer and use it in GitHub Desktop.
Save eneroth/d9c5deb5ab1947dd282c26000b73555f to your computer and use it in GitHub Desktop.
Deep merge with vector, seq and set handling
(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