Of your proposed solutions, this one

 (defn to-consolidated-map [parts]
    (->> parts (map (partial apply hash-map)) (apply merge-with +))

Is the one I thought of first and also find very readable.

On Saturday, August 17, 2013, David Chelimsky wrote:

> Sorry - pressed send before refreshing and didn't see your response.
>
> Readability is a very subjective topic. I have an (possibly irrational)
> aversion to anonymous fns when core fns solve the same problem, so I'm very
> accustomed to reading things like (partial apply xxx). The perf issue would
> definitely push me over, however.
>
>
>
>
> On Sat, Aug 17, 2013 at 11:19 AM, David Chelimsky <dchelim...@gmail.com>wrote:
>
> 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))
>
> Using assoc instead of update-in allows it to handle nils with a default
> value to get. All core lib fns. WDYT?
>
> Also, this:
>
>   (defn to-consolidated-map [parts]
>     (apply merge-with + (map (partial apply hash-map) parts)))
>
> or this variant:
>
>   (defn to-consolidated-map [parts]
>     (->> parts (map (partial apply hash-map)) (apply merge-with +))
>
> each use only core lib fns, which you say you're looking for. I find this
> less accidental-complexity-ish than the implementations that use reduce
> with a custom (anonymous) fn, which each require handling nil in some
> fashion. WDYT?
>
>
>
>
> On Sat, Aug 17, 2013 at 10:42 AM, Steven Degutis <sbdegu...@gmail.com>wrote:
>
> Wow. Sorry for the awful formatting. Re-pasting it from Google Groups
> (instead of email) to fix it:
>
> (def a [[:a 1] [:b 2] [:a 3]])
>
>
>
> (defn add-maybe [& nums]
>
>   (->> nums
>
>        (remove nil?)
>
>        (reduce +)))
>
>
>
> (reduce (fn [m [k
>
>

-- 
-- 
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