Counting occurences (solution to the exercise)
- What does
(merge-with + {:a 12} {:b 4} {:a 3 :b 7})
return? {:b 11, :a 15}
when there are several values for a key, these values are merged (two at a time) using the specified function — here they are summed.- Can you count occurrences of each value in a collection using
merge-with
? (apply merge-with + (map (fn [x] {x 1}) coll))
or, usingfor
:(apply merge-with + (for [x coll] {x 1}))
.