I would like to write a function that checks if a map is a subset of another.
An example of usage should be:
(map-subset? {:a 1 :b 2} {:a 1 :b 2 :c 3})
=> true
Is there a native way to do that?
By converting the maps to sets, you can use clojure.set/subset?
(clojure.set/subset? (set {:a 1 :b 2}) (set {:a 1 :b 2 :c 3}))
=> true
This would make each pair of the map an element in the set
(set {:a 1 :b 2 :c 3})
=> #{[:b 2] [:c 3] [:a 1]}
And as such, {:a 1 :b 3}
would not be a subset
(clojure.set/subset? (set {:a 1 :b 3}) (set {:a 1 :b 2 :c 3}))
=> false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With