Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Commit

Permalink
map-utils: add deep-merge-with
Browse files Browse the repository at this point in the history
  • Loading branch information
Chouser committed Mar 19, 2009
1 parent d94d82f commit 1961302
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/clojure/contrib/map_utils.clj
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,19 @@
[map ks]
(reduce safe-get map ks))

; by Chouser:
(defn deep-merge-with
"Like merge-with, but merges maps recursively, appling the given fn
only when there's a non-map at a particular level.
(deepmerge + {:a {:b {:c 1 :d {:x 1 :y 2}} :e 3} :f 4}
{:a {:b {:c 2 :d {:z 9} :z 3} :e 100}})
-> {:a {:b {:z 3, :c 3, :d {:z 9, :x 1, :y 2}}, :e 103}, :f 4}"
[f & maps]
(apply
(fn m [& maps]
(if (every? map? maps)
(apply merge-with m maps)
(apply f maps)))
maps))

0 comments on commit 1961302

Please sign in to comment.