On Wed, Jan 19, 2011 at 11:23 PM, Stefan Rohlfing
<stefan.rohlf...@gmail.com> wrote:
> Hi all,
>
> I tried another implementation of 'merge-with' using nested calls to
> 'reduce'. However,
> I just cannot get it to work correctly:
>
> (defn my-merge-with [f & maps]
>  (when (some identity
> maps)
>    (reduce
>     (fn [acc m]
>       (reduce
>        (fn [_ [key val]]
>          (assoc acc key (if-let [old-val (get acc key)]
>                                         (f old-val val)
>                                          val)))
>        {} m))
>     {} maps)))

To me, it looks like you're throwing away some results.  (assoc acc
key val) creates a new map with the updated key, it doesn't update the
old one in-place.

Perhaps something like:
(defn my-merge-with [f & maps]
 (when (some identity maps)
   (reduce
    (fn [acc m]
      (reduce
       (fn [acc2 [key val]]
         (assoc acc2 key (if-let [old-val (get acc2 key)]
                                         (f old-val val)
                                          val)))
       acc m))
    {} maps)))

-John

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to