import { optic } from "https://deno.land/x/fun@v2.0.0/mod.ts";
const { filter } = optic;
A composible combinator that can filter or refine the focused value of an existing optic. Care should be taken with this operator as it apples to the modify function as well as the view function. That is to say that if the refinement or predicate returns false for the focused value then that value will not be modified. See the example for clarification.
Examples
Example 1
Example 1
import * as O from "./optic.ts";
import { pipe } from "./fn.ts";
const positive = pipe(O.id<number>(), O.filter(n => n > 0));
const result1 = pipe(positive, O.view(1)); // Some(1);
const result2 = pipe(positive, O.view(0)); // None
const result3 = pipe(1, positive.modify(n => n + 1)); // 2
const result4 = pipe(0, positive.modify(n => n + 1)); // 0
Type Parameters
B extends A
Parameters
r: Predicate<A>