Created
July 8, 2010 19:15
-
-
Save stuarthalloway/468449 to your computer and use it in GitHub Desktop.
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
; Copyright (c) Rich Hickey. All rights reserved. | |
; The use and distribution terms for this software are covered by the | |
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) | |
; which can be found in the file epl-v10.html at the root of this distribution. | |
; By using this software in any fashion, you are agreeing to be bound by | |
; the terms of this license. | |
; You must not remove this notice, or any other, from this software. | |
(defn merge-with* | |
"Like merge-with, but handles initial seed value a la reduce. | |
Unfortunately not signature compatible with current merge-with." | |
([f maps] | |
(when (some identity maps) | |
(let [merge-entry (fn [m e] | |
(let [k (key e) v (val e)] | |
(if (contains? m k) | |
(assoc m k (f (get m k) v)) | |
(assoc m k v)))) | |
merge2 (fn [m1 m2] | |
(reduce merge-entry (or m1 {}) (seq m2)))] | |
(reduce merge2 maps)))) | |
([f initval maps] | |
(when (some identity maps) | |
(let [merge-entry (fn [m e] | |
(let [k (key e) v (val e)] | |
(if (contains? m k) | |
(assoc m k (f (get m k) v)) | |
(assoc m k (f initval v))))) | |
merge2 (fn [m1 m2] | |
(reduce merge-entry (or m1 {}) (seq m2)))] | |
(reduce merge2 {} (seq maps)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment