Skip to main content
Module

x/fun/mod.ts>optics.lens

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

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

Examples

Example 1

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

import * as O from "./optics.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