Re: Sum on a list of maps

2011-10-16 Thread Dave Ray
Yes, just change the grouping function passed to group-by: (group-by #(vector (% "Type") (% "Subtype")) coll) The keys in the resulting map will be two-element vectors (e.g. ["TypeA" "SubTypeA"]) so you'll need to adjust the body of the for a bit in BG's example. Cheers, Dave On Sun, Oct 16

Re: Sum on a list of maps

2011-10-16 Thread der
Thanks BG and all other responses for your help. I have a followup question, could the same approach be extended if I wanted to group-by multiple entries in the map? e.g. assuming the map now had Type and Subtype and I wanted to "group-by" the pair Type and Subtype, would the same approach work?

Re: Sum on a list of maps

2011-10-16 Thread der
Thanks BG and all other responses for your help. I have a followup question, could the same approach be extended if I wanted to group-by multiple entries in the map? e.g. assuming the map now had Type and Subtype and I wanted to "group-by" the pair Type and Subtype, would the same approach work?

Re: Sum on a list of maps

2011-10-14 Thread Jestan Nirojan
I would write like this (defn sum-by-type [coll] (->> coll (map (fn [ {v "Value" t "Type" } ] {t (read-string v) })) (apply (partial merge-with + If the map keyword based, it will be simpler than above (defn sum-by-type [coll] (->> coll (map (fn [ {v :value t :ty

Re: Sum on a list of maps

2011-10-14 Thread Jestan Nirojan
I would write like this (defn sum-by-type [coll] (->> coll (map (fn [ {v "Value" t "Type" } ] {t (read-string v) })) (apply (partial merge-with + If the map is keyword int map, it will be simpler than above (defn sum-by-type [coll] (->> coll (map (fn [ {v :value t :type } ] {t v

Re: Sum on a list of maps

2011-10-14 Thread der
Thanks for all of the useful replies, I'll try them later. Unfortunately I don't control the input list of maps which is the result of a web service. On Oct 14, 10:11 pm, Sean Corfield wrote: > On Fri, Oct 14, 2011 at 10:25 AM, der wrote: > > Given a list of maps of the followign format: ({"Typ

Re: Sum on a list of maps

2011-10-14 Thread Baishampayan Ghose
On Sat, Oct 15, 2011 at 3:16 AM, Alan Malloy wrote: > Probably CSV or some other externally-input data. And FWIW I like BG's > solution - it's what I would have written, except it's better. Thanks Alan :-) Regards, BG -- Baishampayan Ghose b.ghose at gmail.com -- You received this message be

Re: Sum on a list of maps

2011-10-14 Thread Alan Malloy
Probably CSV or some other externally-input data. And FWIW I like BG's solution - it's what I would have written, except it's better. On Oct 14, 2:11 pm, Sean Corfield wrote: > On Fri, Oct 14, 2011 at 10:25 AM, der wrote: > > Given a list of maps of the followign format: ({"Type" "A", "Value" >

Re: Sum on a list of maps

2011-10-14 Thread Sean Corfield
On Fri, Oct 14, 2011 at 10:25 AM, der wrote: > Given a list of maps of the followign format: ({"Type" "A", "Value" > "5"} {"Type" "B", "Value" "4"} {"Type" "A", "Value" "7.2"} {"Type" > "A", "Value" "25.4"} {"Type" "B", "Value" "2.982"}) Folks are posting solutions but I wondered why your map key

Re: Sum on a list of maps

2011-10-14 Thread Baishampayan Ghose
> I'm a Clojure newbie trying to the following simple task: > > Given a list of maps of the followign format: ({"Type" "A", "Value" > "5"} {"Type" "B", "Value" "4"} {"Type" "A", "Value" "7.2"} {"Type" > "A", "Value" "25.4"} {"Type" "B", "Value" "2.982"}) > > I want to compute a list of maps such th

Re: Sum on a list of maps

2011-10-14 Thread Walter van der Laan
You could first turn the Values from String into Double like this: (defn step1 [coll] (map (fn [m] (assoc m "Value" (Double/parseDouble (m "Value" coll)) Next, you can use reduce to calculate the sum per Value: (defn step2 [coll] (reduce (fn [res m] (let [k (m "

Re: Sum on a list of maps

2011-10-14 Thread Dave Ray
Someone cleverer than me to post the built-in function that already does exactly what you want, but here's one way: (defn sum-by-type [in] (->> in (group-by #(get % "Type")) (map (fn [[k vs]] {"Type" k "Value" (reduce (fn [acc v] (+ acc (Double/

Re: Sum on a list of maps

2011-10-14 Thread Islon Scherer
The best i could think was: (defn calc-types [coll] (let [types (map (fn [m] {:type (get m "Type") :value (Double/parseDouble (get m "Value"))}) coll) ; convert keys to keywords and values to doubles it1 (sort-by :type types) ; sort by type a (atom (:type (first it1)))