Ah! fnil! That's just what I was wanting, and didn't even know it. Thanks!
I think I like this version best.

On Mon, Aug 19, 2013 at 2:44 AM, Philipp Meier <phme...@gmail.com> wrote:

>
> Am Samstag, 17. August 2013 11:19:23 UTC+2 schrieb David Chelimsky:
>
>> Hey Steven, here's a variation of my first example that, I think, gets
>> closer to what you're proposing (with maybe-add handled in-line):
>>
>> (defn to-consolidated-map [parts]
>>   (reduce (fn [h [k v]]
>>             (assoc h k (+ (get h k 0) v)))
>>           {}
>>           parts))
>>
>
> An expression of the form (assoc m k (fn (get m k a) v))) can replaced
> with update-in. If you need a default value, then add fnil to the party:
>
> (defn to-consolidated-map [parts]
>   (reduce (fn [h [k v]]
>               (update-in h [k] (fnil + 0) v))
>           {}
>           parts))
>
> -billy,
>
> --
> --
> 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
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to