Skip to content

Instantly share code, notes, and snippets.

@ashenfad
Last active July 7, 2016 17:22
Show Gist options
  • Save ashenfad/cb6ad7bfe94359e813731b39734d48b4 to your computer and use it in GitHub Desktop.
Save ashenfad/cb6ad7bfe94359e813731b39734d48b4 to your computer and use it in GitHub Desktop.
A WhizzML version of Clojure's merge-with
(define (merge-with* fn m1 m2)
(reduce (lambda (result key)
(let (v1 (get m1 key)
v2 (get m2 key))
(assoc result key (if (and v1 v2)
(fn v1 v2)
(or v1 v2)))))
{}
(concat (keys m1) (keys m2))))
(define (merge-with fn . ms)
(reduce (lambda (result m) (merge-with* fn result m))
(head ms)
(tail ms)))
;; whizzml> (merge-with + {"a" 3 "c" 1} {"b" 4 "a" 3 "c" 2} {"c" 8})
;; {"a":6,"c":11,"b":4}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment