Skip to content

Documentation / @eventkit/base / reduce

Function: reduce()

Call Signature

ts
function reduce<V, A>(accumulator): SingletonOperatorFunction<V, V | A>

Applies an accumulator function over the source generator, and returns the accumulated result when the source completes, given an optional seed value.

Like Array.prototype.reduce(), reduce applies an accumulator function against an accumulation and each value emitted by the source generator to reduce it to a single value, emitted on the output generator. This operator also behaves similarly to the reduce method in terms of how it handles the seed value (or initialValue):

  • If no seed value is provided and the observable emits more than one value, the accumulator function will be called for each value starting with the second value, where the first value is used as the initial accumulator value. This means that the accumulator function will be called N-1 times where N is the number of values emitted by the source generator.

  • If the seed value is provided and the observable emits any values, then the accumulator function will always be called starting with the first emission from the source generator.

  • If the observable only emits one value and no seed value is provided, or if the seed value is provided but the observable doesn't emit any values, the solo value will be emitted without any calls to the accumulator function.

  • If the observable emits no values and a seed value is provided, the seed value will be emitted without any calls to the accumulator function.

  • If the observable emits no values and no seed value is provided, the operator will throw a NoValuesError on completion.

Type Parameters

Type ParameterDefault type
V-
AV

Parameters

ParameterTypeDescription
accumulator(acc, value, index) => AThe accumulator function called on each source value.

Returns

SingletonOperatorFunction<V, V | A>

Throws

If the observable emits no values and no seed value is provided.

Call Signature

ts
function reduce<V, A>(accumulator, seed): SingletonOperatorFunction<V, A>

Applies an accumulator function over the source generator, and returns the accumulated result when the source completes, given an optional seed value.

Like Array.prototype.reduce(), reduce applies an accumulator function against an accumulation and each value emitted by the source generator to reduce it to a single value, emitted on the output generator. This operator also behaves similarly to the reduce method in terms of how it handles the seed value (or initialValue):

  • If no seed value is provided and the observable emits more than one value, the accumulator function will be called for each value starting with the second value, where the first value is used as the initial accumulator value. This means that the accumulator function will be called N-1 times where N is the number of values emitted by the source generator.

  • If the seed value is provided and the observable emits any values, then the accumulator function will always be called starting with the first emission from the source generator.

  • If the observable only emits one value and no seed value is provided, or if the seed value is provided but the observable doesn't emit any values, the solo value will be emitted without any calls to the accumulator function.

  • If the observable emits no values and a seed value is provided, the seed value will be emitted without any calls to the accumulator function.

  • If the observable emits no values and no seed value is provided, the operator will throw a NoValuesError on completion.

Type Parameters

Type Parameter
V
A

Parameters

ParameterTypeDescription
accumulator(acc, value, index) => AThe accumulator function called on each source value.
seedAThe initial accumulation value.

Returns

SingletonOperatorFunction<V, A>

Throws

If the observable emits no values and no seed value is provided.

Call Signature

ts
function reduce<V, A, S>(accumulator, seed): SingletonOperatorFunction<V, A>

Applies an accumulator function over the source generator, and returns the accumulated result when the source completes, given an optional seed value.

Like Array.prototype.reduce(), reduce applies an accumulator function against an accumulation and each value emitted by the source generator to reduce it to a single value, emitted on the output generator. This operator also behaves similarly to the reduce method in terms of how it handles the seed value (or initialValue):

  • If no seed value is provided and the observable emits more than one value, the accumulator function will be called for each value starting with the second value, where the first value is used as the initial accumulator value. This means that the accumulator function will be called N-1 times where N is the number of values emitted by the source generator.

  • If the seed value is provided and the observable emits any values, then the accumulator function will always be called starting with the first emission from the source generator.

  • If the observable only emits one value and no seed value is provided, or if the seed value is provided but the observable doesn't emit any values, the solo value will be emitted without any calls to the accumulator function.

  • If the observable emits no values and a seed value is provided, the seed value will be emitted without any calls to the accumulator function.

  • If the observable emits no values and no seed value is provided, the operator will throw a NoValuesError on completion.

Type Parameters

Type ParameterDefault type
V-
A-
SA

Parameters

ParameterTypeDescription
accumulator(acc, value, index) => AThe accumulator function called on each source value.
seedSThe initial accumulation value.

Returns

SingletonOperatorFunction<V, A>

Throws

If the observable emits no values and no seed value is provided.

Released under the MIT License.