import { Option } from "https://deno.land/x/eitherway@0.10.0/lib/core/option.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
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);