Created
September 22, 2016 10:38
-
-
Save stathissideris/7433c03ff980e26b9918e817cb31af21 to your computer and use it in GitHub Desktop.
flexible deep merge implementation in clojure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;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