Reduce
Reduce( reducer, initial, arrayOrSet )
Reduce( reducer, initial, arrayOrSet )
Reduce( reducer, initial, arrayOrSet )
Reduce( reducer, initial, arrayOrSet )
reduce( reducer, initial, arrayOrSet )
Reduce( reducer, initial, arrayOrSet )
Description
The Reduce
function applies a reducer
Lambda
function
serially to each member of arrayOrSet
(which is an Array,
Page, or Set) to produce a single Value (any
Scalar, Array, Object, etc.).
When the reducer
function is called, it is provided with an
accumulator that represents the current state of the result, and the
current item from arrayOrSet
. The value that the reducer
returns
becomes the accumulator for the next iteration. Subsequent invocations
of reducer
can see the results of earlier invocations.
When Reduce
starts, the accumulator is set to the initial
value.
When Reduce
completes its processing, the current value of the
accumulator is returned.
The run time of For query "width" errors, the underlying set or page involves more than
100K items. This can happen when using a set function, such as
For example, instead of:
use:
This does mean that if the entire set must be evaluated to arrive at the
correct result, you would have to page through the For query timeout errors, you may specify a larger query timeout via the driver that you are using. |
Parameters
Parameter | Type | Definition and Requirements |
---|---|---|
|
The function to apply to each item in The function must have the signature
The return value from the |
|
|
The |
|
|
The items to be reduced. |
Examples
The following query demonstrates the simplest reducer, which counts the
items in arrayOrSet
. It does this by adding 1 to the accumulator
for each item:
LongV(5)
5
5
5
5
5
The following query demonstrates how to sum the values in arrayOrSet
.
It does this by adding the value of each item to the accumulator:
LongV(15)
15
15
15
15
15
The following query demonstrates how to count, sum, and compute the minimum and maximum values, plus the average. This time, our accumulator is an object with multiple keys, and our Lambda function now performs all of those calculations:
ObjectV(count: LongV(5),total: LongV(15),min: LongV(1),max: LongV(5),avg: LongV(3))
map[avg:3 count:5 max:5 min:1 total:15]
{count: 5, avg: 3, min: 1, total: 15, max: 5}
{ count: 5, total: 15, min: 1, max: 5, avg: 3 }
{'count': 5, 'total': 15, 'min': 1, 'max': 5, 'avg': 3}
{ count: 5, total: 15, min: 1, max: 5, avg: 3 }
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!