Skip to content

Instantly share code, notes, and snippets.

@tcrayford
Created July 5, 2013 20:53
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 tcrayford/5937179 to your computer and use it in GitHub Desktop.
Save tcrayford/5937179 to your computer and use it in GitHub Desktop.
(defn- merge-entry [mapper m e]
(let [k (key e) v (val e)]
(if (contains? m k)
(assoc m k ((safe-get mapper k) (get m k) v))
(assoc m k v))))
(defn merge-with-mapper
"Returns a map that consists of the rest of the maps conj-ed onto
the first. If a key occurs in more that one map, the mapping(s)
from the latter (left-to-right) will be combined with the mapping in
the result by calling ((mapper key) val-in-result val-in-latter)."
[mapper & maps]
(when (some identity maps)
(let [merge2 (fn [m1 m2]
(reduce (partial merge-entry mapper) (or m1 {}) (seq m2)))]
(reduce merge2 (map #(select-keys % (keys mapper)) maps)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment