Sets
A set object, set, is an unordered collection of distinct values.
-
$set() -> set
$set(arg: iterable) -> set -
Creates a new
setobject. Ifargis iterable, initializes the new set by iterating over its values.
Alternatively, you can create a set by draining an iterator, e.g.
var set = "supercalifragilistic":chars():to_set(); assert set:count() == 12;
Set Literals
You can create a set using literal syntax, e.g.
var set = {1, 2, 3};
Trailing commas are allowed, e.g.
var set = { 1, 2, 3, };
Note that the empty literal {} will create an empty map — use $set() to create an empty set.
Iterating
Sets are iterable:
for item in {1, 2, 3} { echo item; }
Containment
You can check if a set contains an item using the in operator:
var set = {"foo", "bar", "baz"}; if "foo" in set { echo "found"; }
This is equivalent to calling the set's :contains() method.
Set Operators
-
The
|operator returns the union of two sets, e.g.A | B. -
The
&operator returns the intersection of two sets, e.g.A & B. -
The
-operator returns the difference of two sets, e.g.A - B. -
The
^operator returns the symmetric difference of two sets, e.g.A ^ B.
Comparison Operators
-
A <= BistrueifAis a subset ofB. -
A < BistrueifAis a proper subset ofB. -
A >= BistrueifAis a superset ofB. -
A > BistrueifAis a proper superset ofB.
Two sets A and B will compare as equal using the == operator, A == B, if they are set-equivalent, i.e. if they contain the same items in any order.
Methods
-
:add(item: any) -
Adds an item to the set. This is a null operation if the set already contains a member equal to
item. -
:clear() -
Removes all items from the set.
-
:contains(item: any) -> bool -
Returns
trueif the set contains a member equal toitem, otherwisefalse. -
:count() -> i64 -
Returns the number of items in the set.
-
:difference(other: set) -> set -
Returns a new set containing the difference of the two sets — i.e. the set of all items that are in
receiverbut not inother.Equivalent to
receiver - other. -
:intersection(other: set) -> set -
Returns a new set containing the intersection of the two sets — i.e. the set of all items that are in both
receiverandother.Equivalent to
receiver & other. -
:is_empty() -> bool -
Returns
trueif the set is empty. -
:is_equal_to(other: set) -> bool -
Returns
trueifreceiveris set-equivalent toother, i.e. if the two sets contain the same items in any order.Equivalent to
receiver == other. -
:is_proper_subset_of(other: set) -> bool -
Returns
trueifreceiveris a proper subset ofother.Equivalent to
receiver < other. -
:is_proper_superset_of(other: set) -> bool -
Returns
trueifreceiveris a proper superset ofother.Equivalent to
receiver > other. -
:is_subset_of(other: set) -> bool -
Returns
trueifreceiveris a subset ofother.Equivalent to
receiver <= other. -
:is_superset_of(other: set) -> bool -
Returns
trueifreceiveris a superset ofother.Equivalent to
receiver >= other. -
:remove(item: any) -
Removes
itemfrom the set. This is a null operation if the set does not contain a member equal toitem. -
:symmetric_difference(other: set) -> set -
Returns a new set containing the symmetric difference of the two sets — i.e. the set of all items that are either in
receiveror inotherbut not both.Equivalent to
receiver ^ other. -
:union(other: set) -> set -
Returns a new set containing the union of the two sets — i.e. the set of all items that are in either
receiverorother.Equivalent to
receiver | other.