Check out v4 of the Fauna CLI
v4 of the Fauna CLI is now in beta. The new version introduces enhancements to the developer experience, including an improved authentication workflow. To get started, check out the CLI v4 quick start. |
set.reduceRight()
Description
Iterates through each element in a Set to perform a rolling operation. For
example, you can use reduceRight()
to calculate a rolling sum, concatenate
elements, or perform complex transformations.
reduceRight()
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. The last element in the Set serves as the initial accumulator.
-
The current element’s value.
The method returns the result of the last iteration. The calling Set isn’t changed.
Eager loading
This method uses eager loading and requires a read of each document in the calling Set. For large Sets, this may result in poor performance and high costs.
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 |
---|---|---|---|
reducer |
Yes |
Anonymous FQL function to call on each element in the Set. |
Examples
Reduce the Set item, right to left:
// `toSet()` converts an Array to a Set.
let set = [1, 2, 3].toSet()
set.reduceRight((acc, elem) => acc + elem)
6
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!