Thanks for the suggestions Sean and Ben. I learned new functions from both of you!
Sean, your suggestion yields the following: (to-consolidated-map [[:a 1] [:b 2] [:a 3]]) ; {:a (1 3) :b (2)} So it still needs to reduce the vals using +. That led me to this: (defn to-consolidated-map [parts] (apply merge-with + (map (fn [[k v]] {k v}) parts))) Which led me to this: (defn to-consolidated-map [parts] (apply merge-with + (map (partial apply hash-map) parts))) I like this one because it describes the solution the way I thought about it - I just didn't know about merge-with. It also has the benefit of using core fns rather than anonymous fns. WDYT? On Sat, Aug 17, 2013 at 7:23 AM, Sean Corfield <seancorfi...@gmail.com>wrote: > How about this: > > (defn to-consolidated-map [parts] > (apply merge-with concat > (map (fn [[k v]] {k (list v)}) parts))) > > On Fri, Aug 16, 2013 at 9:57 PM, David Chelimsky <dchelim...@gmail.com> > wrote: > > I've got a vector of 2-element vectors e.g. [[:a 1] [:b 2]] where the > first > > val of any vec might appear in another vec e.g. [[:a 1] [:b 2] [:a 3]]. I > > need a fn that will consolidate this into a hash-map with the vals > > consolidated e.g. > > > > (to-consolidated-map [[:a 1] [:b 2] [:a 3]]) > > ; {:a 4 :b 2} > > > > I've got two candidate implementations and I'm curious which you like > better > > and why, or if I'm missing a better way: > > > > (defn to-consolidated-map [parts] > > (reduce (fn [h [k v]] > > (if (contains? h k) > > (assoc h k (+ (k h) v)) > > (assoc h k v))) > > {} parts)) > > > > (defn to-consolidated-map [parts] > > (->> parts > > (group-by first) > > (map (fn [[k v]] [k (->> v (map last) (reduce +))])))) > > > > TIA, > > David > > > > -- > > -- > > 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. > > > > -- > Sean A Corfield -- (904) 302-SEAN > An Architect's View -- http://corfield.org/ > World Singles, LLC. -- http://worldsingles.com/ > > "Perfection is the enemy of the good." > -- Gustave Flaubert, French realist novelist (1821-1880) > > -- > -- > 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.