title: Clojure: summing values in a collection of maps
body: <p>I am trying to sum up values of a collection of maps by their common keys. I have this snippet:</p>
  
  <pre><code>(def data [{:a 1 :b 2 :c 3} {:a 1 :b 2 :c 3}]
  (for [xs data] (map xs [:a :b]))
  ((1 2) (1 2))
  
  Final result should be ==&gt; (2 4)
  </code></pre>
  
  <p>Basically, I have a list of maps. Then I perform a list of comprehension to take only the keys I need.</p>
  
  <p>My question now is how can I now sum up those values? I tried to use "reduce" but it works only over sequences, not over collections.</p>
  
  <p>Thanks.</p>
  
  <p>===EDIT====</p>
  
  <p>Using the suggestion from Joost I came out with this:</p>
  
  <pre><code>(apply merge-with + (for [x data] (select-keys x [:col0 :col1 :col2]))
  </code></pre>
  
  <p>This iterates a collection and sums on the chosen keys. The "select-keys" part I added is needed especially to avoid to get in trouble when the maps in the collection contain literals and not only numbers.</p>