Reduce

Not available in this language yet.
Not available in this language yet.
Reduce( reducer, initial, collection )
Reduce( reducer, initial, collection )
Not available in this language yet.
Not available in this language yet.
Reduce( reducer, initial, collection )
Not available in this language yet.

Description

The Reduce function applies a reducer Lambda function serially to each member of the collection (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 the collection. 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.

Parameters

Argument Type Definition and Requirements

reducer

The function to apply to each item in the collection.

The function must have the signature f( accumulator, value ), where:

  • accumulator is the current state of the result

  • value is the current item from the collection

The return value from the reducer function becomes the accumulator for the next iteration.

initial

Any

The initial parameter sets the initial value of the accumulator.

collection

Array or Set

The collection of items to be reduced.

Returns

The value of the accumulator, which would typically be the same type as the initial value.

Examples

The following query demonstrates the simplest reducer, which counts the items in the collection. It does this by adding 1 to the accumulator for each item:

Not available in this language yet.
Not available in this language yet.
System.out.println(
    client.query(
        Reduce(
            Lambda(
                Arr(Value("acc"), Value("val")),
                Add(Var("acc"), Value(1))
            ),
            0,
            Arr(1, 2, 3, 4, 5)
        )
    ).get());
client.query(
  q.Reduce(
    q.Lambda((acc, value) => q.Add(acc, 1)),
    0,
    [ 1, 2, 3, 4, 5 ]
  )
)
.then(ret => console.log(ret))
Not available in this language yet.
Not available in this language yet.
println(Await.result(
  client.query(
    Reduce(
      Lambda(Arr("acc", "val"), Add(Var("acc"), 1)),
      0,
      Arr(1, 2, 3, 4, 5)
    )
  ),
  5.seconds
))
Not available in this language yet.
5
5
5

The following query demonstrates how to sum the values in the collection. It does this by adding the value of each item to the accumulator:

Not available in this language yet.
Not available in this language yet.
System.out.println(
    client.query(
        Reduce(
            Lambda(
                Arr(Value("acc"), Value("value")),
                Add(Var("acc"), Var("value"))
            ),
            0,
            Arr(1, 2, 3, 4, 5)
        )
    ).get());
client.query(
  q.Reduce(
    q.Lambda((acc, value) => q.Add(acc, value)),
    0,
    [ 1, 2, 3, 4, 5 ]
  )
)
.then(ret => console.log(ret))
Not available in this language yet.
Not available in this language yet.
println(Await.result(
  client.query(
    Reduce(
      Lambda(Arr("acc", "value"), Add(Var("acc"), Var("value"))),
      0,
      Arr(1, 2, 3, 4, 5)
    )
  ),
  5.seconds
))
Not available in this language yet.
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:

Not available in this language yet.
Not available in this language yet.
System.out.println(
    client.query(
        Reduce(
            Lambda(
                Arr(Value("acc"), Value("value")),
                Let(
                    "count", Add(Select("count", Var("acc")), Value(1))
                    "total", Add(Select("total", Var("acc")), value)
                    "min", Select("min", Var("acc"))
                    "max", Select("max", Var("acc"))
                ).in(
                    Obj(
                        "count", Var("count"),
                        "total", Var("total"),
                        "min", If(LTE(Var("value"), Var("min")), Var("value"), Var("min")),
                        "max", If(GTE(Var("value"), Var("max")), Var("value"), Var("max")),
                        "avg", Divide(Var("total"), Var("count"))
                    )
                )
            ),
            Obj(
              "count", 0,
              "total", 0,
              "min", 999999,
              "max", -999999,
              "avg", 0,
            ),
            Arr( 1, 2, 3, 4, 5 )
        )
    ).get());
client.query(
  q.Reduce(
    q.Lambda((acc, value) => q.Let(
      {
        count: q.Add(q.Select("count", acc), 1),
        total: q.Add(q.Select("total", acc), value),
        min: q.Select("min", acc),
        max: q.Select("max", acc),
      },
      {
        count: q.Var("count"),
        total: q.Var("total"),
        min: q.If(q.LTE(value, q.Var("min")), value, q.Var("min")),
        max: q.If(q.GTE(value, q.Var("max")), value, q.Var("max")),
        avg: q.Divide(q.Var("total"), q.Var("count"))
      }
    )),
    {
      count: 0,
      total: 0,
      min: 999999,
      max: -999999,
      avg: 0,
    },
    [ 1, 2, 3, 4, 5 ]
  )
)
.then(ret => console.log(ret))
Not available in this language yet.
Not available in this language yet.
println(Await.result(
  client.query(
    Reduce(
      Lambda {
        (acc, value) => Let {
          val count = Add(Select("count", acc), 1)
          val total = Add(Select("total", acc), value)
          val min   = Select("min", acc)
          val max   = Select("max", acc)
          Obj(
            "count" -> count,
            "total" -> total,
            "min"   -> If(LTE(value, min), value, min),
            "max"   -> If(GTE(value, max), value, max),
            "avg"   -> Divide(total, count)
          )
        }
      },
      Obj(
        "count" -> 0,
        "total" -> 0,
        "min"   -> 999999,
        "max"   -> -999999,
        "avg"   -> 0,
      ),
      Arr( 1, 2, 3, 4, 5 )
    )
  ),
  5.seconds
))
Not available in this language yet.
{count: 5, avg: 3, min: 1, total: 15, max: 5}
{ count: 5, total: 15, min: 1, max: 5, avg: 3 }
{count: 5, avg: 3, min: 1, total: 15, max: 5}

Was this article helpful?

We're sorry to hear that.
Tell us how we can improve! documentation@fauna.com

Thank you for your feedback!