diff options
-rw-r--r-- | src/clj/clojure/set.clj | 16 | ||||
-rw-r--r-- | test/clojure/test_clojure/clojure_set.clj | 26 |
2 files changed, 41 insertions, 1 deletions
diff --git a/src/clj/clojure/set.clj b/src/clj/clojure/set.clj index 2d861511..835e6063 100644 --- a/src/clj/clojure/set.clj +++ b/src/clj/clojure/set.clj @@ -139,6 +139,22 @@ ret))) #{} s)))) +(defn subset? + "Is set1 a subset of set2?" + {:added "1.2", + :tag Boolean} + [set1 set2] + (and (<= (count set1) (count set2)) + (every? set2 set1))) + +(defn superset? + "Is set1 a superset of set2?" + {:added "1.2", + :tag Boolean} + [set1 set2] + (and (>= (count set1) (count set2)) + (every? set1 set2))) + (comment (refer 'set) (def xs #{{:a 11 :b 1 :c 1 :d 4} diff --git a/test/clojure/test_clojure/clojure_set.clj b/test/clojure/test_clojure/clojure_set.clj index 54173692..1aa745a4 100644 --- a/test/clojure/test_clojure/clojure_set.clj +++ b/test/clojure/test_clojure/clojure_set.clj @@ -179,4 +179,28 @@ (deftest test-map-invert (are [x y] (= x y) - (set/map-invert {:a "one" :b "two"}) {"one" :a "two" :b}))
\ No newline at end of file + (set/map-invert {:a "one" :b "two"}) {"one" :a "two" :b})) + +(deftest test-subset? + (are [sub super] (set/subset? sub super) + #{} #{} + #{} #{1} + #{1} #{1} + #{1 2} #{1 2} + #{1 2} #{1 2 42}) + (are [notsub super] (not (set/subset? notsub super)) + #{1} #{} + #{2} #{1} + #{1 3} #{1})) + +(deftest test-superset? + (are [super sub] (set/superset? super sub) + #{} #{} + #{1} #{} + #{1} #{1} + #{1 2} #{1 2} + #{1 2 42} #{1 2}) + (are [notsuper sub] (not (set/superset? notsuper sub)) + #{} #{1} + #{2} #{1} + #{1} #{1 3})) |