method StreamClass.prototype.chunkWhile
Private
StreamClass.prototype.chunkWhile<A, C>(
initial: A,
step: (
value: T,
accumulator: A
) => { acc: A; emit?: C; halt?: boolean; }
,
flush?: (accumulator: A) => C | undefined
): StreamClass<C, E>

The general chunker — Elixir's chunk_while/4, object-shaped for JS: step returns { acc } to keep accumulating, { acc, emit } to emit a chunk, plus halt: true to end the stream; flush may emit one last chunk from the final accumulator. Values only; failures pass through.

const batchesOfTwo = Stream.from([1, 2, 3, 4, 5]).chunkWhile(
  [] as number[],
  (n, acc) => (acc.length === 1 ? { acc: [], emit: [...acc, n] } : { acc: [...acc, n] }),
  (acc) => (acc.length > 0 ? acc : undefined),
);
await batchesOfTwo.collect(); // [[1, 2], [3, 4], [5]]

Type Parameters

Parameters

initial: A
step: (
value: T,
accumulator: A
) => { acc: A; emit?: C; halt?: boolean; }
optional
flush: (accumulator: A) => C | undefined

Return Type

Usage

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