Sets
A set object, set
, is an unordered collection of distinct values.
-
$set() -> set
$set(arg: iterable) -> set
-
Creates a new set. If
arg
is 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":runes():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, e.g.
for item in {1, 2, 3} { echo item; }
Containment
You can check if a set contains an item using the in
operator, e.g.
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 <= B
istrue
ifA
is a subset ofB
. -
A < B
istrue
ifA
is a proper subset ofB
. -
A >= B
istrue
ifA
is a superset ofB
. -
A > B
istrue
ifA
is 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
true
if 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
receiver
but 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
receiver
andother
.Equivalent to
receiver & other
. -
:is_empty() -> bool
-
Returns
true
if the set is empty. -
:is_equal_to(other: set) -> bool
-
Returns
true
ifreceiver
is 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
true
ifreceiver
is a proper subset ofother
.Equivalent to
receiver < other
. -
:is_proper_superset_of(other: set) -> bool
-
Returns
true
ifreceiver
is a proper superset ofother
.Equivalent to
receiver > other
. -
:is_subset_of(other: set) -> bool
-
Returns
true
ifreceiver
is a subset ofother
.Equivalent to
receiver <= other
. -
:is_superset_of(other: set) -> bool
-
Returns
true
ifreceiver
is a superset ofother
.Equivalent to
receiver >= other
. -
:remove(item: any)
-
Removes
item
from 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
receiver
or inother
but 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
receiver
orother
.Equivalent to
receiver | other
. -
:values() -> iter
-
Returns an iterator over the set's values.