Skip to main content
Module

x/fun/optic.ts>filter

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

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

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

A
B extends A

Returns

<U extends Tag, S>(first: Optic<U, S, A>) => Optic<Align<U, AffineTag>, S, B>

Returns

<U extends Tag, S>(first: Optic<U, S, A>) => Optic<Align<U, AffineTag>, S, A>