method StreamClass.prototype.reduce
Private
StreamClass.prototype.reduce<A>(
fn: (
accumulator: A,
value: T,
index: number
) => A | PromiseLike<A>
,
initial: A
): Task<A, E>

Folds every value into one, fail-fast like StreamClass#collect — the terminal counterpart to StreamClass#scan's lazy running fold, and the reason a ten-million-row stream can answer with a single number.

Without it the only route is (await stream.collect()).reduce(…), which buffers the whole source to produce one value and gives back everything the module exists to avoid. Elixir does not need a member here because Enum.reduce accepts any Enumerable; JS has no such fallback.

initial is required. The seedless form would have to raise on an empty stream — the way [].reduce(fn) does — and a stream's emptiness is not knowable before it is drained, so the failure would arrive at the worst possible moment. Naming the seed also names A.

const total = await Stream.from([1, 2, 3]).reduce((sum, n) => sum + n, 0);
total; // 6

Type Parameters

Parameters

fn: (
accumulator: A,
value: T,
index: number
) => A | PromiseLike<A>
initial: A

Return Type

Task<A, E>

Usage

import { StreamClass } from "lib/stream/stream.ts";