Skip to main content
Module

x/fun/optic.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/optic.ts";

Construct a Fold<S, A> from view and modify functions.

Examples

Example 1

import * as O from "./optic.ts";
import * as R from "./record.ts";

const values = <A>() => O.fold<Record<string, A>, A>(
  Object.values,
  R.map,
);

const numberValues = values<number>();

const result1 = numberValues.view({}); // []
const result2 = numberValues.view({ foo: 1 }); // [1]
const result3 = numberValues.modify(n => n + 1)({}); // {}
const result4 = numberValues.modify(n => n + 1)({ foo: 1 }); // { foo: 2 }

Parameters

view: (s: S) => ReadonlyArray<A>
modify: (modifyFn: (a: A) => A) => (s: S) => S