Reputation: 105
I have two hash maps and am trying to merge them together while only keeping the keys found in both maps.
Ex:
{a true, b true, c true, d true, e true}
merged with {c true, d true, e true, f true}
would yield {c true, d true, e true}
I am pretty new to Clojure and just can't seem to figure out how to do this. Thanks
Upvotes: 2
Views: 331
Reputation: 414
Apart from select-keys, min-key is also useful. This function can be used with an arbitrary number of map
(defn min-merge [& ms]
(let [min-keys (keys (apply min-key count ms))]
(select-keys (apply merge ms) min-keys)))
Try
user=> (min-merge {:a 1 :b 2} {:a 3} {:a 4 :b 5 :c 6})
{:a 4}
Upvotes: 1
Reputation: 83680
There is a select-keys function in the standard library
(let [a {:a true :b true :c true :d true :e true}
b {:c true :d true :e true :f true}
b-keys (keys b)]
(select-keys a b-keys))
#=> {:c true, :d true, :e true}
Upvotes: 7