Skip to main content
Module

x/fun/mod.ts>optic.fromPredicate

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

Construct a Prism<S, A> from a Refinement<S, A>.

Examples

Example 1

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

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

const isNonEmpty = <A>(arr: ReadonlyArray<A>): arr is NonEmptyArray<A> =>
  arr.length > 0;
const noninit = O.fromPredicate(isNonEmpty<number>);

const result1 = noninit.view([]); // None
const result2 = noninit.view([1]); // Some([1]) as NonEmptyArray
const result3 = noninit.review([1]); // [1] Cast NonEmptyArray as Array

Type Parameters

S
A extends S

Parameters

refinement: Refinement<S, A>

Construct a Prism<A, A> from a Predicate<A, A>.

Examples

Example 1

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

const positive = O.fromPredicate((n: number) => n > 0);

const result1 = positive.view(1); // Some(1)
const result2 = positive.view(0); // None
const result3 = positive.review(0); // 0
const result4 = positive.modify(n => n + 1)(0); // 0
const result5 = positive.modify(n => n + 1)(1); // 2

Parameters

predicate: Predicate<A>