Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a map is a subset of another in clojure?

Tags:

clojure

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?

like image 516
szymanowski Avatar asked Dec 06 '13 10:12

szymanowski


1 Answers

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
like image 157
maxthoursie Avatar answered Sep 29 '22 16:09

maxthoursie