Skip to content

Instantly share code, notes, and snippets.

@ponzao
Last active January 22, 2016 17:21
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 ponzao/634c7add8014bf6fcdff to your computer and use it in GitHub Desktop.
Save ponzao/634c7add8014bf6fcdff to your computer and use it in GitHub Desktop.
(defn merge-with-key
[f & maps]
(when (some identity maps)
(reduce (fn [acc x]
(reduce (fn [m [k v]]
(if (contains? m k)
(assoc m k (f k (get m k) v))
(assoc m k v)))
acc
x))
maps)))
(defn deep-merge-with-key
[f & maps]
(apply
(fn m [& maps]
(let [[k & args] maps]
(cond (every? map? maps) (apply merge-with-key m maps)
(every? map? args) (apply merge-with-key m args)
:else (apply f k args))))
maps))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment