Re: Sum up second elemsts of pairs in a sequence by grouping by the first item in pair

2015-03-21 Thread Fluid Dynamics
Or (reduce (partial merge-with +) (map (partial apply hash-map) in-seq))... -- 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 patien

Re: Sum up second elemsts of pairs in a sequence by grouping by the first item in pair

2015-03-20 Thread Francis Avila
Yet another way: (vec (reduce (fn [m [k v]] (assoc m k (+ (m k 0) v))) {} [[1 0.5] [1 0.7] [2 1.0] [3 0.1] [3 0.1]])) => [[3 0.2] [2 1.0] [1 1.2]] On Friday, March 20, 2015 at 8:45:10 AM UTC-5, Emrehan Tüzün wrote: > > Yet another way to solve it: > > > > > > > > >

Re: Sum up second elemsts of pairs in a sequence by grouping by the first item in pair

2015-03-20 Thread Gary Johnson
Sometimes, all you need is the proper reduce formulation: (reduce (fn [m [k v]] (assoc m k (+ (m k 0) v))) {} keyvals) -- 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 fro

Re: Sum up second elemsts of pairs in a sequence by grouping by the first item in pair

2015-03-20 Thread Emrehan Tüzün
Yet another way to solve it: *user=> x[[1 0.5] [1 0.7] [2 1.0] [3 0.1] [3 0.1]]user=> (group-by first x){1 [[1 0.5] [1 0.7]], 2 [[2 1.0]], 3 [[3 0.1] [3 0.1]]}user=> (map #(vector (first %) (second %)) (group-by first x))([1 [[1 0.5] [1 0.7]]] [2 [[2 1.0]]] [3 [[3 0.1] [3 0.1]]])user=

Re: Sum up second elemsts of pairs in a sequence by grouping by the first item in pair

2015-03-19 Thread Erick Pintor
Just one more way to solve it but, getting a hash-map as a result (->> [[1 0.5] [1 0.7] [2 1.0] [3 0.1] [3 0.1]] (map (partial apply hash-map)) (apply merge-with +)) Em quinta-feira, 19 de março de 2015 19:02:28 UTC-3, Ambrose Bonnaire-Sergeant escreveu: > > user=> (def a (group-by

Re: Sum up second elemsts of pairs in a sequence by grouping by the first item in pair

2015-03-19 Thread Ambrose Bonnaire-Sergeant
user=> (def a (group-by first [[1 0.5] [1 0.7] [2 1.0] [3 0.1] [3 0.1]])) #'user/a user=> (for [[k vs] a] [k (apply + (map second vs))]) ([1 1.2] [2 1.0] [3 0.2]) On Thu, Mar 19, 2015 at 3:41 PM, Alex wrote: > Hello everybody, > > How to transform sequence > > *[[1 0.5] [1 0.7] [2 1.0] [3 0.1]

Sum up second elemsts of pairs in a sequence by grouping by the first item in pair

2015-03-19 Thread Alex
Hello everybody, How to transform sequence *[[1 0.5] [1 0.7] [2 1.0] [3 0.1] [3 0.1]]* to *[[1 1.2] [2 1.0] [3 0.2]]* ? Best regards, Alex -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegrou