Skip to main content
Module

x/fun/optics.ts>apply

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 apply
import { apply } from "https://deno.land/x/fun@v2.0.0-alpha.12/optics.ts";

Apply the value returned by a Viewer to a function returned by a Viewer.

Examples

Example 1

import * as O from "./optics.ts";
import { pipe } from "./fn.ts";

type Person = { name: string, age: number };
type State = { people: readonly Person[], format: (p: Person) => string };

const fmt = pipe(O.id<State>(), O.prop("format"));
const adults = pipe(
  O.id<State>(),
  O.prop("people"),
  O.array,
  O.filter(p => p.age > 18)
);

const formatted = pipe(fmt, O.apply(adults));

const result = formatted.view({
  people: [
    { name: "Brandon", age: 37 },
    { name: "Rufus", age: 1 },
  ],
  format: p => `${p.name} is ${p.age}`,
}); // [ "Brandon is 37" ]

Type Parameters

V extends Tag
S
A

Parameters

second: Viewer<V, S, A> | Optic<V, S, A>

Returns

<U extends Tag, I>(first: Viewer<U, S, (a: A) => I> | Optic<U, S, (a: A) => I>) => Viewer<Align<U, V>, S, I>