Available since: v3.0.0
Set functions
These functions can be used when working with, and manipulating sets of data.
set::add
The set::add function adds an item to a set.
API DEFINITION
set::add(set, $new_val: value) -> set
If the item is not present in the set, it will be added.
{"one", "two"}.add("three");
Otherwise, the original set will be returned.
{"one", "two"}.add("two");
set::complement
The set::complement function returns the complement of two sets, namely a single set containing items which are not in the second set.
API DEFINITION
set::complement(set, $other: set) -> set
Example with output
{1, 2, 3, 4}.complement({3, 4, 5, 6});
set::contains
The set::contains function checks to see if a value is contained within a set.
API DEFINITION
set::concat(set, $other: set) -> set
Example with output
set::difference
The set::difference function determines the difference between two sets, returning a single set containing items which are not in both sets.
API DEFINITION
set::difference(set, $other: set) -> set
Example with output
{1, 2, 3, 4}.difference({3, 4, 5, 6});
set::intersect
The set::intersect function calculates the values which intersect two sets, returning a single set containing the values which are in both sets.
API DEFINITION
set::intersect(set, $other: set) -> set
Example with output
{1, 2, 3, 4}.intersect({3, 4, 5, 6});
set::is_empty
The set::is_empty function checks whether the set is empty or not.
API DEFINITION
set::is_empty(set) -> bool
Example with output
set::len
The set::len function calculates the length of a set, returning a number. This function includes all items when counting the number of items in the set. If you want to only count truthy values, then use the count() function.
API DEFINITION
set::len(set) -> number
Example with output
{1, 2, 1, null, "something", 3, 3, 4, 0}.len();
set::remove
The set::remove function removes an item of a certain value from a set.
API DEFINITION
set::remove(set, $remove: value) -> set
The following example removes the value 2 from a set.
{1, 2, 2, 2, 5}.remove(2);
If the value does not exist, the untouched set will be returned.
Removing
{1, 2, 2, 2, 5}.remove(3);
set::union
The set::union function combines two sets together, removing duplicate values, and returning a single set.
API DEFINITION
set::union(set, $other: set) -> set
Example with output
{1, 2, 1, 6}.union({1, 3, 4, 5, 6});