I am new to Clojure. My question is that how to get a intersection of two hash-maps, e.g.
(def map-1 {"a" 2, "b" 1, "c" 4, "d" 3})
(def map-2 {"a" 3, "b" 6, "e" 5})
As we defined two maps, the expected result would be {"a" 3, "b" 6}
, it is in a map with intersected keys and the max value of the key.
Somehow I came up with a solution and implemented it, but it works partially correct.
Basic idea is that find the map which has the smallest amount of items in it, use it as a reference. For each item in the reference map to check whether the other map contains it. If it contains, put the key to output map with its max value (using (max num1 num2)
)
The following is my sample code:
(defn max-intersect [map1 map2]
(let [smaller-map (if (< (count map1) (count map2))
map1
map2)
longer-map (if (= smaller-map map1)
map2
map1)]
(loop [output {}
reference-map smaller-map]
(if (empty? reference-map)
output
(recur (let [[item-key item-val] (first smaller-map)]
(when (contains? longer-map item-key)
(assoc output item-key (max item-val (get longer-map item-key)))))
(rest reference-map))))))
This is my repl result:
test-intersect.core=> (def map1 {"a" 2, "b" 1, "c" 4, "d" 3})
#'test-intersect.core/map1
test-intersect.core=> (def map2 {"a" 3, "b" 6, "e" 5})
#'test-intersect.core/map2
test-intersect.core=> (max-intersect map1 map2)
{"a" 3}
It may seem complex, I am also waiting for any great and efficient solutions.
Thanks a lot!