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.reduce()

Reduce a Set to a single, accumulated value by applying a provided function to each element. Iterates through elements from left to right. Uses the first element as the initial value.

Signature

reduce(reducer: ((A, A) => A)) => A | Null

Description

Iterates through each element in a Set to perform a rolling operation. For example, you can use reduce() to calculate a rolling sum, concatenate elements, or perform complex transformations.

reduce() calls a reducer callback function on every element of the Set from left to right. The reducer function takes two arguments:

  • The accumulator that holds the running result from previous iterations. The first 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.

Performance hint: full_set_read

Queries that call this method on a document Set emit a performance hint, if enabled. For example, the following query:

// Use `reduce()` to sum stock counts
let stockCounts = Product.all().map(doc => doc.stock)
stockCounts.reduce((a, b) => a + b)

Emits the following hint:

performance_hint: full_set_read - Using reduce() causes the full set to be read. See https://docs.fauna.com/performance_hint/full_set_read.
at *query*:3:19
  |
3 | stockCounts.reduce((a, b) => a + b)
  |                   ^^^^^^^^^^^^^^^^^
  |

To address the hint, use set.take() to explicitly limit the size of the calling Set to fewer than 100 documents:

// Limit the doc Set's size using `take()`
let stockCounts = Product.all().take(20).map(doc => doc.stock)
stockCounts.reduce((a, b) => a + b)

This applies even if the original, unbounded Set contains fewer than 100 documents.

Alternatively, you can rewrite the query to avoid calling the method.

Avoid use on large sets

This method scans the full Set, which can cause many reads and might time out for large Sets.

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:

Parameters

Parameter Type Required Description

reducer

Function

Yes

Anonymous FQL function to call on each element in the Set.

Reducer function parameters:

Parameter Type Required Description

accumulator

Generic

Yes

Value returned by the previous reducer function call. On the first call, seed is passed as the accumulator.

current

Generic

Yes

The current element’s value.

Return value

One of:

Type Description

Generic

Result of the last reducer function call.

Null

Returns Null if the calling Set is empty.

Examples

  1. Starting with the following product prices:

    // Gets a Set of the first nine `Product` collection
    // documents and projects the `price` field.
    Product.all().take(9) { price }
    {
      data: [
        {
          price: 698
        },
        {
          price: 2499
        },
        {
          price: 499
        },
        {
          price: 399
        },
        {
          price: 35
        },
        {
          price: 349
        },
        {
          price: 299
        },
        {
          price: 149
        },
        {
          price: 2399
        }
      ]
    }
  2. Use reduce() to find the maximum price in the Set:

    Product.all().take(9).reduce((s, v) => {
      if (v.price > s.price) { v } else { s }
    }) {price}
    {
      price: 2499
    }

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!