ClojureDocs

导航

命名空间

超集?

clojure.set

自 1.2 起可用
  • (superset? set1 set2)
Is set1 a superset of set2?
2 Examples
(use '[clojure.set :only [superset?]])

user=> (superset? #{0} #{0})
true

user=> (superset? #{0 1} #{0})
true

user=> (superset? #{0} #{0 1}) 
false
;; Advice: Do not call superset? with non-set arguments.  If you are
;; concerned that you may be unintentionally doing so, and want an
;; exception to be thrown if you do, consider using the library
;; funjible (https://github.com/jafingerhut/funjible) which provides
;; its own definition of superset? that is identical to Clojure's,
;; except it checks the types of its arguments.

;; superset? might or might not return what you expect if you give it
;; values that are not sets.  The implementation of superset? _does not
;; check_ whether you are actually giving it values that are sets.  It
;; _assumes_ so.

;; If the first argument were a set with the same elements, you would
;; expect the return value false here.
user=> (superset? [2 4 6 8] #{1 3})
true

;; Here, if the first argument were a set with the same elements as
;; the vector, you would expect the return value true.
user=> (superset? [1 3 5] #{1 3 5})
false

;; And similarly here:
user=> (superset? ["1" "3" "5"] #{"1" "3"})
false

;; Switching to considering cases where the second argument is not a set, this
;; appears to do what one would hope:
user=> (superset? #{1 2 3 4 5} [1 2 3 4])
true

;; ... but this does not (at least up until Clojure 1.10 this behavior is
;; because of how superset? is implemented, first comparing the count of the
;; two collections to make it possible to quickly return false when the second
;; collection is larger):
user=> (subset? #{1 2 3 4 5} [1 2 3 4 1 2 3 4])
false

;; Why not change the definition of superset? so it always throws
;; an exception if you give it a non-set argument?  I would guess that
;; the primary reason is that the extra run-time type checks would
;; slow superset? down by an amount that the Clojure core team does
;; not want everyone to have to pay on every such call.

;; Related Clojure tickets: 
;; https://dev.clojure.org/jira/browse/CLJ-1953
;; https://dev.clojure.org/jira/browse/CLJ-2287
See Also

Is set1 a subset of set2?

Returns a set of the distinct elements of coll.

0 Notes
No notes for superset?