Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/fun/optic.ts>affineFold

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

Construct an AffineFold<S, A> from view and modify functions.

Examples

Example 1

import type { Either } from "./either.ts";

import * as O from "./optic.ts";
import * as E from "./either.ts";

const right = <R, L = unknown>() => O.affineFold<Either<L, R>, R>(
  E.getRight,
  E.map,
);

const numberRight = right<number, string>();

const result1 = numberRight.view(E.right(1)); // Some(1)
const result2 = numberRight.view(E.left("Hello")); // None
const result3 = numberRight.modify(n => n + 1)(E.right(1)); // Right(2)
const result4 = numberRight.modify(n => n + 1)(E.left("Hello"));
// Left("Hello")

Parameters

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