Clojure: summing values in a collection of maps

后端 未结 1 1489
滥情空心
滥情空心 2021-01-19 07:38

I am trying to sum up values of a collection of maps by their common keys. I have this snippet:

(def data [{:a 1 :b 2 :c 3} {:a 1 :b 2 :c 3}]
(for [xs data]          


        
1条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-19 08:08

    If you really want to sum the values of the common keys you can do the whole transformation in one step:

    (apply merge-with + data)
    => {:a 2, :b 4, :c 6}
    

    To sum the sub sequences you have:

    (apply map + '((1 2) (1 2)))
    => (2 4)
    

    0 讨论(0)
提交回复
热议问题