fold()

Call the reducer for every element of an set, using a seed as the first acc (accumulator).

Signature

fold(seed: Any, reducer: (acc: Any, value: Any) => Any): Any

Description

The fold() method calls reducer for every element of the set, using a seed as the first acc (accumulator).

The source set isn’t changed.

Parameters

Parameter Type Required Description

seed

Any

Yes

Initial state value provided to the reducer Function.

reducer

Function

Yes

Anonymous Function to invoke for each Set value.

reducer parameters:

Parameter Type Required Description

acc

Any

Yes

Value returned by the previous invocation of reducer. On the first invocation of reducer, seed is passed as the acc (accumulator).

value

Any

Yes

Current Set value.

Return value

Type Description

Any

Result of the last reducer invocation. The seed is returned for an empty set.

Examples

let iter = [1, 2, 3]
iter.fold(100, (value, elem) => value + elem)
106

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!