Skip to content

Function: scan()

scan<T, R>(accumulator, seed): Operator<T, R>

Defined in: operators/scan.ts:20

Creates a stream operator that accumulates values from the source stream, emitting each intermediate accumulated result.

This operator is stateful and is ideal for scenarios where you need to maintain a running total or build a state object over the life of a stream. It takes a seed value and an accumulator function. For each value from the source, it applies the accumulator and emits the new result immediately.

Type Parameters

T

T = any

The type of the values in the source stream.

R

R = any

The type of the accumulated value and the output stream.

Parameters

accumulator

(acc, value, index) => CallbackReturnType<R>

The function that combines the current accumulated value with the new value from the source. This function can be synchronous or asynchronous.

seed

R

The initial value for the accumulator.

Returns

Operator<T, R>

An Operator instance that can be used in a stream's pipe method.

Examples

From scan.spec.ts:4

typescript
const testStream = from([1, 2, 3]);
const accumulator = (acc: number, value: number) => acc + value;
const seed = 0;
const scannedStream = testStream.pipe(scan(accumulator, seed));
let results: any[] = [];
scannedStream.subscribe({
  next: (value) => results.push(value),
  complete: () => {
    expect(results).toEqual([1, 3, 6]);
    done();
  },
});

Released under the MIT License.