Skip to main content
Module

x/eitherway/mod.ts>Option.apply

Yet Another Option and Result Implementation - providing safe abstractions for fallible flows inspired by F# and Rust
Latest
function Option.apply
import { Option } from "https://deno.land/x/eitherway@0.10.0/mod.ts";
const { apply } = Option;

Use this to apply an Option<T> to a handler of type Option<MapFn>

fn( arg ) arg: Some arg: None
fn: Some Some<MapFn> None
fn: None None None

This emulates the typical behavior of Applicative in functional languages

NOTE: Some<T> and None are not applicative functors as this capability is exposed via the type/namespace and not the instances

See Applicative

Examples

Example 1

import { assert } from "./assert.ts";
import { Option, None, Some } from "./option.ts";

type UserRecord = {
  name: string;
  email: string;
  role: string;
  org: string;
  lastSeen: Date;
  scopes: string[];
}
const record: UserRecord = {
  name: "Allen",
  email: "allen@example.com",
  role: "Staff",
  org: "Sales",
  lastSeen: new Date(2023,2, 23),
  scopes: ["read:sales", "write:sales", "read:customer"],
}

const extractScopes = (rec: UserRecord): string[] => rec.scopes;
const maybeAction = Option.from(extractScopes);
const maybeRec = Option.from(record);

const maybeScopes = Option.apply(maybeAction, maybeRec);

assert(maybeScopes.isSome() === true);

Type Parameters

Args extends Readonly<unknown>
R

Parameters

fn: Option<(args: Args) => R>
arg: Option<Args>