Skip to main content

Reducer<V, A>

Reactive function that can be used to accumulate each key's values.

EagerCollection.reduce accepts a constructor function of a top-level class that implements this Reducer interface. For each key in a collection, a Reducer can compute and maintain an accumulated value of the key's values as associations are added and removed from the collection.

Type Parameters

Type ParameterDescription
V extends JsonType of input values.
A extends JsonType of accumulated and output values.

Methods

add()

add(accum, value): A

Include a new value into the accumulated value.

Parameters

ParameterTypeDescription
accumNullable<A>The current accumulated value; null only if initial is null and value is the first to be added.
valueV & DepSafeThe added value.

Returns

A

The updated accumulated value.


remove()

remove(accum, value): Nullable<A>

Exclude a previously added value from the accumulated value.

It is always valid for remove to return null, in which case the correct accumulated value will be computed using initial and add on each of the key's values.

WARNING: If remove returns a non-null value, then it must be equal to calling add on each of the values associated to the key, starting from initial. That is, accum, add(remove(accum, value), value), and remove(add(accum, value), value) must all be equal for any accum and value unless both the mentioned remove calls return null.

Parameters

ParameterTypeDescription
accumAThe current accumulated value.
valueV & DepSafeThe removed value.

Returns

Nullable<A>

The updated accumulated value, or null indicating that the accumulated value should be recomputed using add and initial.

Properties

initial

initial: Nullable<A>;

Initial accumulated value, providing the accumulated value for keys that are not associated to any values.