set.foldRight()
Reduce a set to a single, accumulated value by applying a function to each element. Iterates through elements from right to left. Uses a provided seed as the initial value.
Description
The foldRight()
method iterates through each element in a set to perform
a rolling operation. For example, you can use foldRight()
to calculate a
rolling sum, concatenate elements, or perform complex transformations.
foldRight()
calls a reducer callback function on every element of the set
from right to left. The reducer function takes two arguments:
-
The accumulator that holds the running result from previous iterations. For the first iteration, a seed value serves as the initial accumulator.
-
The current element’s value from the set.
The method returns the result of the last iteration. The calling set isn’t changed.
Fold family methods
FQL supports several methods for folds, which iteratively reduce a set to a single value. These methods include:
The methods are similar but have the following differences:
-
set.fold()
andset.foldRight()
accept an initial seed value and use it as the initial accumulator.set.reduce()
andset.reduceRight()
use the set’s first element as the initial accumulator. -
set.fold()
andset.reduce()
iterate through the set’s elements from left to right.set.foldRight()
andset.reduceRight()
iterate through the set’s elements from right to left.
Parameters
Parameter | Type | Required | Description |
---|---|---|---|
seed |
Yes |
Initial accumulator value provided to the reducer function. |
|
reducer |
Yes |
Anonymous FQL function to call on each element of the set. |
Return value
Type | Description |
---|---|
Result of the last reducer function call. For an empty set, the seed is returned. |
Examples
// `toSet()` converts an array to a set.
let set = [1, 2, 3].toSet()
set.foldRight(100, (value, elem) => value + elem)
106
Is this article helpful?
Tell Fauna how the article can be improved:
Visit Fauna's forums
or email docs@fauna.com
Thank you for your feedback!