> 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 that each type appears once in
> the list and the value of the type is the sum of the values, so that
> in the example it would be:
>
> ({"Type", "A", "Value", "37.6"} {"Type", "B", "Value", "6.982"})
>
> Any ideas?

I might write it like this -

(defn sum-by-type [coll]
    (for [[k v] (group-by #(% "Type") coll)]
        {"Type" k "Value" (apply + (map (comp #(Float/parseFloat %)
#(% "Value")) v))}))

Regards,
BG

-- 
Baishampayan Ghose
b.ghose at gmail.com

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

Reply via email to