Skip to content

Instantly share code, notes, and snippets.

@favila
Created February 28, 2020 22:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save favila/3809b350b74f745b43daecff95484cc8 to your computer and use it in GitHub Desktop.
Save favila/3809b350b74f745b43daecff95484cc8 to your computer and use it in GitHub Desktop.
merge, but throws on conflicts
(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