1 - using java.util.HashMap
given n maps,
user=> (def map1 (java.util.HashMap. {:reference "user1" :type "Promotion" :included-scans 100}))
#'user/map1
user=> (def map2 (java.util.HashMap. {:reference "user1" :type "Promotion" :included-scans 50}))
#'user/map2
map over vector of hash-maps,
user=> (map #(:included-scans %) [map1 map2]) ;; #() represents a function on each element
(100 50)
;; alternative way is
user=> (map :included-scans [map1 map2])
(100 50)
sum the values using reduce
function,
user=> (reduce + (map #(:included-scans %) [map1 map2]))
150
Note that above map is java.lang.HashMap
, clojure has its own data-structure as well called clojure.lang.PersistentHashMap
2 - Using clojure.lang.PersistentHashMap
,
given n number of maps,
user=> (def map1 (hash-map :reference "user1" :type "Promotion" :included-scans 100))
#'user/map1
user=> (def map2 (hash-map :reference "user1" :type "Promotion" :included-scans 50))
#'user/map2
You simply can map over them and get the required key which is :included-scans
in your case,
user=> (map #(:included-scans %) [map1 map2])
(100 50)
Then to sum up using reduce
function,
user=> (reduce + (map #(:included-scans %) [map1 map2]))
150
references
https://clojuredocs.org/clojure.core/hash-map
Clojure: working with a java.util.HashMap in an idiomatic Clojure fashion