Skip to main content
Module

x/fun/mod.ts>array.fold

A collection of algebraic data types, lenses, and schemables based on a light weight higher kinded type implementation. Written for deno.
Latest
function array.fold
import { array } from "https://deno.land/x/fun@v2.0.0/mod.ts";
const { fold } = array;

Reduce an array from left to right, accumulating into a type O via the function foao: (O, A, index) => O and an initial value O.

Examples

Example 1

import * as A from "./array.ts";
import { pipe } from "./fn.ts";

const result = pipe(
  A.range(5, 1),
  A.fold((sum, value, index) => sum + value + index, 0),
);
// 0 + 0 + 0 = 0
// 0 + 1 + 1 = 2
// 2 + 2 + 2 = 6
// 6 + 3 + 3 = 12
// 12 + 4 + 4 = 20
// 20

Parameters

foao: (
o: O,
a: A,
i: number,
) => O
o: O

Returns

(ua: ReadonlyArray<A>) => O