import { fromPredicate } from "https://deno.land/x/fun@v2.0.0-alpha.10/optics.ts";
Construct a Prism<S, A> from a Refinement<S, A>.
Examples
Example 1
Example 1
import type { NonEmptyArray } from "./array.ts";
import * as O from "./optics.ts";
const isNonEmpty = <A>(arr: ReadonlyArray<A>): arr is NonEmptyArray<A> =>
arr.length > 0;
const nonempty = O.fromPredicate(isNonEmpty<number>);
const result1 = nonempty.view([]); // None
const result2 = nonempty.view([1]); // Some([1]) as NonEmptyArray
const result3 = nonempty.review([1]); // [1] Cast NonEmptyArray as Array
Type Parameters
A extends S
Parameters
refinement: Refinement<S, A>
Construct a Prism<A, A> from a Predicate<A, A>.
Examples
Example 1
Example 1
import * as O from "./optics.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