Skip to content

Commit

Permalink
promote Jason Wolfe's superset?, subset? #338
Browse files Browse the repository at this point in the history
Signed-off-by: Stuart Halloway <stu@thinkrelevance.com>
  • Loading branch information
stuarthalloway committed May 7, 2010
1 parent c4eb571 commit f769f56
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/clj/clojure/set.clj
Expand Up @@ -139,6 +139,22 @@
ret)))
#{} s))))

(defn subset?
"Is set1 a subset of set2?"
{:added "1.2",
:tag Boolean}
[set1 set2]

This comment has been minimized.

Copy link
@harold

harold Jul 28, 2016

Might prefer subset/superset for these argument names. (:

(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}
Expand Down
26 changes: 25 additions & 1 deletion test/clojure/test_clojure/clojure_set.clj
Expand Up @@ -179,4 +179,28 @@

(deftest test-map-invert
(are [x y] (= x y)
(set/map-invert {:a "one" :b "two"}) {"one" :a "two" :b}))
(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}))

0 comments on commit f769f56

Please sign in to comment.