FQL v4 will be decommissioned on June 30, 2025. Ensure that you complete your migration from FQL v4 to FQL v10 by that date. Fauna accounts created after August 21, 2024 must use FQL v10. These accounts will not be able to run FQL v4 queries or access the v4 Dashboard. For more details, see the v4 EOL announcement and migration guide. Contact support@fauna.com with any questions. |
Reduce
This reference topic applies to FQL v4. Go to this page for the latest FQL v10 reference topics. |
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 |
|
|
Any |
The |
|
Array, Page, or Set |
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:
5
5
5
LongV(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:
15
15
15
LongV(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:
{ count: 5, total: 15, min: 1, max: 5, avg: 3 }
{'count': 5, 'total': 15, 'min': 1, 'max': 5, 'avg': 3}
map[avg:3 count:5 max:5 min:1 total:15]
ObjectV(count: LongV(5),total: LongV(15),min: LongV(1),max: LongV(5),avg: LongV(3))
{count: 5, avg: 3, min: 1, total: 15, max: 5}
{ 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!