Operations on sets
Factor handbook > The language > Collections > Sets

Prev:set? ( object -- ? )
Next:Set implementations


To test if an object is a member of a set:
in? ( elt set -- ? )


All sets can be represented as a sequence, without duplicates, of their members:
members ( set -- seq )


To get the number of elements in a set:
cardinality ( set -- n )


Sets can have members added or removed destructively:
adjoin ( elt set -- )

delete ( elt set -- )

clear-set ( set -- )

union! ( set1 set2 -- set1 )

diff! ( set1 set2 -- set1 )

intersect! ( set1 set2 -- set1 )


To test if a set is the empty set:
null? ( set -- ? )


Basic mathematical operations, which any type of set may override for efficiency:
diff ( set1 set2 -- set )

intersect ( set1 set2 -- set )

union ( set1 set2 -- set )


Mathematical predicates on sets, which may be overridden for efficiency:
intersects? ( set1 set2 -- ? )

subset? ( set1 set2 -- ? )

set= ( set1 set2 -- ? )


An optional generic word for creating sets of the same class as a given set:
set-like ( set exemplar -- set' )


An optional generic word for creating a set with a fast lookup operation, if the set itself has a slow lookup operation:
fast-set ( set -- set' )


For set types that allow duplicates, like sequence sets, some additional words test for duplication:
all-unique? ( set -- ? )

duplicates ( set -- seq )


Utilities for sets and sequences:
within ( seq set -- subseq )

without ( seq set -- subseq )