Skip to main content
Using Deno in production at your company? Earn free Deno merch.
Give us feedback
Module

x/fun/option.ts>fold

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

Reduce over an Option. Since an Option contains at most one value this function operates a lot like getOrElse. If the passed option is None then it returns the initial value, otherwise the foldr function is called with both the initial value and the inner A.

Examples

Example 1

import * as O from "./option.ts";

const fold = O.fold((n: number, m: number) => n + m, 0);

const result1 = fold(O.some(1)); // 1
const result2 = fold(O.none); // 0

Parameters

foldr: (accumulator: O, current: A) => O
initial: O

Returns

(ua: Option<A>) => O