Created
February 28, 2020 22:11
-
-
Save favila/3809b350b74f745b43daecff95484cc8 to your computer and use it in GitHub Desktop.
merge, but throws on conflicts
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
(defn safe-merge | |
"Like merge, but throws if maps have the same keys but different values." | |
[& maps] | |
(reduce | |
(fn [m [m2k m2v :as re]] | |
(if-some [[_ mv :as le] (find m m2k)] | |
(if (= mv m2v) | |
m | |
(throw (ex-info "Attempted to safe-merge maps with conflicting entries" | |
{:left-entry le :right-entry re}))) | |
(conj m re))) | |
(first maps) (apply concat (rest maps)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment