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

x/fun/mod.ts>optic.lens

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

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

Examples

Example 1

import type { NonEmptyArray } from "./array.ts";

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

const head = <A>() => O.lens<NonEmptyArray<A>, A>(
  ([head]) => head,
  mod => ([head, ...rest]) => [mod(head), ...rest],
);

const headNum = head<number>();

const result1 = headNum.view(A.array(1, 2, 3)); // 1
const result2 = headNum.modify((n: number) => n + 100)(A.array(1, 2, 3));
// [100, 2, 3]

Parameters

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