reduce
reduce() function
Also known as foldl, this method boils down a list of values into a single value.
Signature:
declare function reduce<A extends readonly []>(f: Arrow, iterable: A): undefined;
Returns:
undefined
Example
const sum = (a:number, b:number) => a + b;
reduce(sum, [1, 2, 3, 4]); // 10
reduce(sum, 0, [1, 2, 3, 4]); // 10
// with pipe
pipe(
[1, 2, 3, 4],
map(a => a + 10),
filter(a => a % 2 === 0),
reduce(sum),
); // 26
await pipe(
Promise.resolve([1, 2, 3, 4]),
map((a) => a + 10),
filter(a => a % 2 === 0),
reduce(sum),
); // 26
// if you want to use asynchronous callback
await pipe(
Promise.resolve([1, 2, 3, 4]),
toAsync,
map(async (a) => a + 10),
filter(a => a % 2 === 0),
reduce(sum),
); // 26
// with toAsync
await pipe(
[Promise.resolve(1), Promise.resolve(2), Promise.resolve(3), Promise.resolve(4)],
toAsync,
map(a => a + 10),
filter(a => a % 2 === 0),
reduce(sum),
); // 26