Skip to main content
Module

x/fun/mod.ts>newtype.prism

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

If the Newtype and its underlying value are not referentially transparent (meaning they can always be swapped) then you can create an instance of Prism for the Newtype in order to optionally map into the Newtype given some Predicate.

Examples

Example 1

import type { Option } from "./option.ts";

import * as N from "./newtype.ts";
import * as O from "./option.ts";
import { pipe } from "./fn.ts";

type Integer = N.Newtype<"Integer", number>;

const prismInteger = N.prism<Integer>(Number.isInteger);

// Turn number into an Option<Integer>
const result1: Option<Integer> = prismInteger.view(1);

// Turn an Option<Integer> into a number or fall back to 0 if Option is None
const result2: number = pipe(
  result1,
  O.map(prismInteger.review),
  O.getOrElse(() => 0)
);

Parameters

predicate: Predicate<ToValue<T>>

Returns

Prism<ToValue<T>, T>