method Enumerable.prototype.reduce
Enumerable.prototype.reduce(reduceFn: (
acc: T,
item: T,
index: number,
) => T | Promise<T>
): Promise<T>

Reduce the sequence to a single value.

Executes a reducer function on each element, passing the accumulated result from one element to the next.

Examples

Sum numbers

import { range } from "jsr:@j50n/proc";

const sum = await range({ from: 1, until: 5 })
  .reduce((acc, n) => acc + n, 0);
// 15

Parameters

reduceFn: (
acc: T,
item: T,
index: number,
) => T | Promise<T>

The reducer function.

Return Type

Promise<T>

The final accumulated value.

Throws

TypeError if the iteration is empty and no initial value provided.

Enumerable.prototype.reduce<U>(
reduceFn: (
acc: U,
item: T,
index: number,
) => U | Promise<U>
,
zero: U,
): Promise<U>

Reduce the sequence to a single value with an initial value.

Type Parameters

Parameters

reduceFn: (
acc: U,
item: T,
index: number,
) => U | Promise<U>

The reducer function.

zero: U

The initial accumulator value.

Return Type

Promise<U>

The final accumulated value.