API · Aggregate

reduce1()

Combine all successful values using the first one as the initial accumulator.

Signature

reduce1(
  fn: (accumulator: T, value: T, context: C) => T,
): Exstream<T, AggregateOutputContext<C, T>>

Example

const maximum = await exstream(scores)
  .reduce1((best, score) => Math.max(best, score))
  .value()

Parameters

fn

Type (accumulator, value, context) => TRequired

Called synchronously from the second successful value onward. The first value becomes the accumulator without invoking the callback.

Behavior

The final accumulator is emitted when upstream ends. The current runtime emits one undefined value for an empty source; avoid relying on that edge case and use reduce(fn, initialValue) when empty input is possible. The public TypeScript return type remains T.

Memory is constant apart from the accumulator and materialized contexts. Input order is preserved; existing record errors pass through and are excluded. The output has an aggregate context for successful inputs.

Errors

A thrown reducer error becomes a contextual record error and terminates this reduction branch without a result. Returned promises are not awaited.

Forms

stream.reduce1(reducer)
exstream.pipeline().reduce1(reducer)
exstream.reduce1(reducer, stream)
stream.through(exstream.reduce1(reducer))

reduce(), asyncReduce(), last()