method Stream.scan
Stream.scan<U>(
fn: (
acc: U,
value: T,
) => Promisable<U>
,
initialValue: U,
): Stream<U, E>

Like fold but emits the running accumulator after each successful value, allowing downstream operations to react to intermediate states.

Examples

Example 1

import { Source } from "anabranch";

const stream = Source.from<number, Error>(async function* () {
  yield 1;
  yield 2;
  yield 3;
});

const scanned = stream.scan((sum, n) => sum + n, 0);
// Emits: { type: "success", value: 1 }, { type: "success", value: 3 }, { type: "success", value: 6 }

Type Parameters

Parameters

fn: (
acc: U,
value: T,
) => Promisable<U>
initialValue: U

Return Type

Stream<U, E>

See

Usage

import { type Stream } from ".";