import { reduce } from "https://deno.land/x/aitertools@0.6.0/src/fold.ts";
Apply reducer
function of two arguments cumulatively to the elements of
an async iterable source
, from left to right, so as to reduce the async
iterable to a single value.
import { reduce } from "./fold.ts";
async function* oneToFive() { yield 1; yield 2; yield 3; yield 4; yield 5; }
const reducedValue = await reduce(
(x, y) => { console.log(`${x} + ${y} = ${x + y}`); return x + y; },
oneToFive(),
0,
);
console.log("reduced value:", reducedValue);
The above example will print the following:
0 + 1 = 1
1 + 2 = 3
3 + 3 = 6
6 + 4 = 10
10 + 5 = 15
reduced value: 15
If the async iterable source
is empty, the reducer
function is not
called and initialValue
is returned.
import { reduce } from "./fold.ts";
const reducedValue = await reduce(
(x, y) => { console.log(`${x} + ${y} = ${x * y}`); return x * y; },
[],
1,
);
console.log("reduced value:", reducedValue);
The above example will print the following:
reduced value: 1
Parameters
A binary operation that accepts the previous reduction result and the next element in the async iterable, and returns the next reduction result.
The async iterable to reduce over the elements into a single value. It should be finite, and can be empty.
The initial value of the accumulator. It's usually
the identity element for the reducer
function,
e.g., 0
for (x, y) => x + y
, or 1
for
(x, y) => x * y
. If source
is empty, this value
is returned.
Apply reducer
function of two arguments cumulatively to the elements of
an async iterable source
, from left to right, so as to reduce the async
iterable to a single value.
import { reduce } from "./fold.ts";
async function* oneToFive() { yield 1; yield 2; yield 3; yield 4; yield 5; }
const reducedValue = await reduce<number, number>(
(x, y) => { console.log(`${x} + ${y} = ${x + y}`); return x + y; },
oneToFive(),
);
console.log("reduced value:", reducedValue);
The above example will print the following:
1 + 2 = 3
3 + 3 = 6
6 + 4 = 10
10 + 5 = 15
reduced value: 15
If the async iterable source
is empty, it throws an TypeError
.
import { reduce } from "./fold.ts";
const reducedValue = await reduce<number, number>(
(x, y) => { console.log(`${x} + ${y} = ${x * y}`); return x * y; },
[],
);
console.log("reduced value:", reducedValue);
The above example will print the following:
Uncaught TypeError: reduce without initialValue requires at least one element
at reduce ...
at async ...
Parameters
A binary operation that accepts the previous reduction result and the next element in the async iterable, and returns the next reduction result.