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}))
(apply (partial merge-with +))))
Jestan.
On Oct 14, 10:25 pm, der <[email protected]> wrote:
> Hi,
>
> 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?
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en