Skip to main content
Module

x/fun/option.ts>mapNullable

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

Apply a mapping function to an Option but if the mapping function returns null or undefined the null or undefined value is lifted into None.

Examples

Example 1

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

const result1 = pipe(
  O.some([1, 2, 3]),
  O.mapNullable(arr => arr[10]),
); // None
const result2 = pipe(
  O.constNone<Array<number>>(),
  O.mapNullable(arr => arr[0]),
); // None
const result3 = pipe(
  O.some([1, 2, 3]),
  O.mapNullable(arr => arr[0]),
); // Some(1)

Parameters

f: (a: A) => I | null | undefined

Returns

(ua: Option<A>) => Option<I>