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 :type } ] {t v}))
       (apply (partial merge-with +)))

Jestan Nirojan.
On Oct 15, 12:46 am, Dave Ray <dave...@gmail.com> wrote:
> 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/parseDouble (get v "Value"))))
>                     0
>                     vs)}))))
>
> All the strings don't help much.
>
> Cheers,
>
> Dave
>
>
>
>
>
>
>
> On Fri, Oct 14, 2011 at 1:25 PM, der <derealme.derea...@gmail.com> 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 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 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