A set is a collection which is unordered and can only contain things. Each thing will only exists once in a collection.


Function Description
add Add things to a set.
clear Remove all things from a set.
copy Copy a set (same as dup unless a non-zero deep argument is used).
dup Duplicate a set.
each Iterate over all items in a set.
every Check if all things pass a given test.
filter Return a new set with things that pass a given test.
find Return the first thing which passes a given test.
has Test if a set contains a given thing.
len Return the length of a set.
map Return a list with the results of calling a provided closure on every thing.
one Return one thing from the set.
reduce Execute a reducer function on each thing, resulting in a single output value.
remove Remove things from a set. Accepts one or more things to remove or a closure to be used as a test. The removed things are returned in a list.
restriction Return the restriction of the set or nil when the set is not restricted.
some Check if at least one thing passes a given test.


Operation Description
| (union) Set with things from both a and b.
& (intersection) Set with things common to a and b.
- (difference) Set with things in a but not in b.
^ (symmetric difference) Set with things in either a or b but not both.

Example set operators

anna = {};
cato = {};
iris = {};

a = set(cato, iris);
b = set(cato, anna);

assert (a | b == set(anna, cato, iris));    // Union
assert (a & b == set(cato));                // Intersection
assert (a - b == set(iris));                // Difference
assert (a ^ b == set(anna, iris));          // Symmetric difference

Be careful using assignment operators on stored sets. Although set operations are processed very efficient, a change to a stored set requires a change. This change* is still an assignment and will therefore contain the complete resulting set.
In practice this means that it is perfect to write something like set_a |= set_b, but avoid using a stored set like .set_a |= set_b.

Function Description
set Create a new empty set or convert a list to a new set.