Skip to content

Instantly share code, notes, and snippets.

@stathissideris
Created September 22, 2016 10:38
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 stathissideris/7433c03ff980e26b9918e817cb31af21 to your computer and use it in GitHub Desktop.
Save stathissideris/7433c03ff980e26b9918e817cb31af21 to your computer and use it in GitHub Desktop.
flexible deep merge implementation in clojure
;;found here: https://github.com/metosin/ring-swagger/blob/1c5b8ab7ad7a5735624986bbb6b288aaf168d407/src/ring/swagger/common.clj#L53-L73
(defn deep-merge
"Recursively merges maps.
If the first parameter is a keyword it tells the strategy to
use when merging non-map collections. Options are
- :replace, the default, the last value is used
- :into, if the value in every map is a collection they are concatenated
using into. Thus the type of (first) value is maintained."
{:arglists '([strategy & values] [values])}
[& values]
(let [[values strategy] (if (keyword? (first values))
[(rest values) (first values)]
[values :replace])]
(cond
(every? map? values)
(apply merge-with (partial deep-merge strategy) values)
(and (= strategy :into) (every? coll? values))
(reduce into values)
:else
(last values))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment