Set functions
These functions can be used when working with, and manipulating sets of data.
| Function | Description |
|---|---|
set::add() | Adds an item to a set if it does not exist |
set::all() | Checks if all elements in a set match a condition |
set::any() | Checks if any elements in a set match a condition |
set::at() | Accesses the element at a specific position in a set |
set::complement() | Returns the complement of two sets |
set::contains() | Checks to see if a value is present in a set |
set::difference() | Returns the difference between two sets |
set::filter() | Filters elements in a set that match a condition |
set::find() | Finds the first element in a set that matches a condition |
set::find() | Gets the first element in a set |
set::flatten() | Flattens nested sets and arrays into a single set |
set::fold() | Folds over a set with an accumulator |
set::intersect() | Returns the values which intersect two sets |
set::is_empty() | Checks if a set is empty |
set::join() | Joins set elements into a string with a separator |
set::last() | Gets the last element in a set |
set::len() | Returns the length of a set |
set::map() | Maps over the elements of a set to return a new set |
set::max() | Returns the greatest value from a set |
set::min() | Returns the least value from a set |
set::reduce() | Reduces a set via a closure |
set::remove() | Removes an item at a specific position from a set |
set::slice() | Removes an item at a specific position from a set |
set::union() | Returns the unique merged values from two sets |
set::add
The set::add function adds an item to a set only if it does not already exist.
The following example shows this function, and its output, when used in a RETURN statement:
A set can also be extended with the contents of an array or another set.
set::all
When called on a set without any extra arguments, the set::all function checks whether all set values are truthy.
The following example shows this function, and its output, when used in a RETURN statement:
The set::all function can also be followed with a value or a closure to check whether all elements conform to a condition.
set::any
The set::any function checks whether any set values are truthy.
When called on a set without any extra arguments, the set::any function checks whether any set values are truthy.
The set::any function can also be followed with a value or a closure to check whether any elements conform to a condition.
set::at
The set::at function returns the value at the specified only, or in reverse for a negative index.
Because sets are ordered, the position of the item is based on the set's sorted order.
The following example shows this function, and its output, when used in a RETURN statement:
You can also pass in a negative index.
set::complement
The set::complement function returns the complement of two sets, namely a single set containing items that are in the first set but not in the second set.
set::contains
The set::contains function checks to see if a value is contained within a set.
set::difference
The set::difference function determines the symmetric difference between two sets, returning a single set containing items that are not shared between them.
set::filter
The set::filter function filters out values that do not match a pattern.
The following example shows a simple use of set::filter() with a closure.
You can also pass a value to keep only exact matches.
set::find
The set::find function returns the first matching value from a set.
Because sets are ordered, the first matching value is the first match in the set's sorted order.
The following example shows this function, and its output, when used in a RETURN statement:
The set::find function is most useful when a closure is passed in, which allows for customized searching.
set::first
The set::first function returns the first value from a set.
Because sets are ordered, this returns the least value in the set's sorted order.
The following example shows this function, and its output, when used in a RETURN statement:
set::flatten
The set::flatten function flattens nested sets and arrays into a single set.
Note that this function will remove a single layer of nesting, and may need to be called more than once if you have a set with another set or array inside it.
set::fold
The set::fold function applies an operation on an initial value and every element in the set, returning the final result.
This function is commonly used to sum or otherwise accumulate values from a set.
Because set::fold() takes an explicit initial value, it is useful when the result type should differ from the type of the set values.
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.
set::is_empty
The set::is_empty function checks whether the set is empty or not.
set::join
The set::join function joins all values in a set together into a string, with a string separator in between each value.
Because sets are ordered, the joined string follows the set's sorted order.
set::last
The set::last function returns the last value from a set.
Because sets are ordered, this returns the greatest value in the set's sorted order.
The following example shows this function, and its output, when used in a RETURN statement:
set::len
The set::len function calculates the length of a set, returning a number. This function counts unique items only.
If you want to only count truthy values, then use the count() function.
set::map
The set::map function allows the user to call an anonymous function (closure) that is performed on every item in the set before passing it on.
Because the result is also a set, duplicate mapped values are removed.
The most basic use of set::map involves choosing a parameter name for each item in the set and a desired output.
An example of mapping several values to the same result.
set::max
The set::max function returns the greatest value from a set of values.
The following example shows this function, and its output, when used in a RETURN statement:
As any value can be compared with another value, the set can contain any SurrealQL value.
set::min
The set::min function returns the least value from a set of values.
The following example shows this function, and its output, when used in a RETURN statement:
As any value can be compared with another value, the set can contain any SurrealQL value.
set::reduce
The set::reduce function applies an operation on every element in the set, returning the final result.
If you need an initial value to pass in before the other items are operated on, use the set::fold function instead.
This function is commonly used to sum or perform some other mathematical operation on the items in a set.
Another example showing set::reduce() used to build a string:
set::remove
The set::remove function removes a value from a set.
The following example removes the value 2 from a set.
If the value does not exist, the untouched set will be returned.
You can also remove the contents of an array or another set.
set::slice
The set::slice function returns a slice of a set by position.
Because sets are ordered, slicing is based on the set's sorted order.
The following example shows this function, and its output, when used in a RETURN statement:
set::union
The set::union function combines two sets together, removing duplicate values, and returning a single set.