// Absence as a value. `{ tag: "some", value }` or `{ tag: "none" }`. // // ply does not export this type at the pinned commit (package.json) or at ply main // on 2026-11-01. This module is the local copy of that shape. When a pin does export // it, re-export ply's module from this file and delete the local definitions. // Callers keep importing `src/option.ts` either way. // // The platform's absent value is not a member of this type. Convert at `src/absent.ts`. export type Some = { readonly tag: "some"; readonly value: T }; export type None = { readonly tag: "none" }; export type Option = Some | None; const NONE: None = Object.freeze({ tag: "none" }); export function some(value: T): Some { return Object.freeze({ tag: "some", value }); } export function none(): Option { return NONE; } export function isSome(option: Option): option is Some { return option.tag === "some"; } export function isNone(option: Option): option is None { return option.tag === "none"; } export function map(option: Option, fn: (value: T) => U): Option { if (option.tag === "none") return none(); return some(fn(option.value)); } export function flatMap(option: Option, fn: (value: T) => Option): Option { if (option.tag === "none") return none(); return fn(option.value); } export function filter(option: Option, keep: (value: T) => boolean): Option { if (option.tag === "none") return none(); if (!keep(option.value)) return none(); return option; } export function unwrapOr(option: Option, fallback: T): T { if (option.tag === "none") return fallback; return option.value; } export function orElse(first: Option, second: Option): Option { if (first.tag === "some") return first; return second; } /** JSON and other hosts that use null for absence. A present value is returned as itself. */ export function orNull(option: Option): T | null { if (option.tag === "none") return null; return option.value; } export function match(option: Option, onSome: (value: T) => U, onNone: () => U): U { if (option.tag === "some") return onSome(option.value); if (option.tag === "none") return onNone(); return absurd(option); } /** First element for which `keep` holds. A finished scan is none; the host's find is not used. */ export function find(items: readonly T[], keep: (item: T) => boolean): Option { for (const item of items) { if (keep(item)) return some(item); } return none(); } function absurd(value: never): never { throw new Error(`unhandled option: ${JSON.stringify(value)}`); }