Skip to content

Instantly share code, notes, and snippets.

@gerritjvv
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gerritjvv/9bd082b905d56aef3526 to your computer and use it in GitHub Desktop.
Save gerritjvv/9bd082b905d56aef3526 to your computer and use it in GitHub Desktop.
The map is formed of key = topic, value = stats map of numeric stats The task was to aggregate the numeric values in the stats map for similar topics, each message could have multiple topics.
(def msgs [ {"a" {:stat1 1 :stat2 10} "b" {:stat1 1}} {"a" {:stat1 1}}])
(def merged (apply merge-with (partial merge-with +) msgs))
;; {"a" {:stat1 2, :stat2 10}, "b" {:stat1 1}}
(prn merged)
;; {"a" {:stat1 2, :stat2 10}, "b" {:stat1 1}}
(= (-> merged first second :stat1) 2)
;; true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment