// ets_tracing: off /* adapted from https://github.com/gcanti/fp-ts */ import * as Tp from "../Collections/Immutable/Tuple/index.js" import type { Either } from "../Either/core.js" import type { Lazy, Predicate, Refinement } from "../Function/core.js" import { identity } from "../Function/core.js" import * as St from "../Structural/index.js" import type { HasUnify } from "../Utils/index.js" const _noneHash = St.hashString("@effect-ts/system/Option/None") const _someHash = St.hashString("@effect-ts/system/Option/Some") /** * Definitions */ export class None implements HasUnify { readonly _tag = "None"; [St.equalsSym](that: unknown): boolean { return that instanceof None } get [St.hashSym](): number { return _noneHash } } export class Some implements HasUnify { readonly _tag = "Some" constructor(readonly value: A) {} [St.equalsSym](that: unknown): boolean { return that instanceof Some && St.equals(this.value, that.value) } get [St.hashSym](): number { return St.combineHash(_someHash, St.hash(this.value)) } } export type Option = None | Some /** * Constructs none */ export const none: Option = new None() /** * Constructs none */ export function emptyOf(): Option { return none } /** * Constructs Some(A) */ export function some(a: A): Option { return new Some(a) } /** * Classic applicative */ export function ap_(fab: Option<(a: A) => B>, fa: Option): Option { return isNone(fab) ? none : isNone(fa) ? none : some(fab.value(fa.value)) } /** * Classic applicative * * @ets_data_first ap_ */ export function ap(fa: Option) { return (fab: Option<(a: A) => B>): Option => ap_(fab, fa) } /** * Zips `Option[A]` and `Option[B]` into `Option[(A, B)]` */ export function zip_(fa: Option, fb: Option): Option> { return chain_(fa, (a) => map_(fb, (b) => Tp.tuple(a, b))) } /** * Zips `Option[A]` and `Option[B]` into `Option[(A, B)]` * * @ets_data_first zip_ */ export function zip(fb: Option) { return (fa: Option): Option> => zip_(fa, fb) } /** * Apply both and return first * * @ets_data_first zipFirst_ */ export function zipFirst(fb: Option) { return (fa: Option): Option => zipFirst_(fa, fb) } /** * Apply both and return first */ export function zipFirst_(fa: Option, fb: Option): Option { return ap_( map_(fa, (a) => () => a), fb ) } /** * Apply both and return second * * @ets_data_first zipSecond_ */ export function zipSecond(fb: Option) { return (fa: Option): Option => zipSecond_(fa, fb) } /** * Apply both and return second */ export function zipSecond_(fa: Option, fb: Option): Option { return ap_( map_(fa, () => (b: B) => b), fb ) } /** * Builds a new option constructed using the value of self */ export function chain_(self: Option, f: (a: A) => Option): Option { return isNone(self) ? none : f(self.value) } /** * Builds a new option constructed using the value of self * * @ets_data_first chain_ */ export function chain(f: (a: A) => Option) { return (self: Option): Option => chain_(self, f) } /** * Like chain but ignores the constructed outout * * @ets_data_first tap_ */ export function tap(f: (a: A) => Option) { return (ma: Option): Option => chain_(ma, (a) => map_(f(a), () => a)) } /** * Like chain but ignores the constructed outout */ export function tap_(ma: Option, f: (a: A) => Option): Option { return chain_(ma, (a) => map_(f(a), () => a)) } /** * Flattens nested options */ export function flatten(fa: Option>): Option { return chain_(fa, identity) } /** * Wraps this option into a second one */ export function duplicate(ma: Option): Option> { return isNone(ma) ? none : some(ma) } /** * Returns `true` if the predicate is satisfied by the wrapped value * * @ets_data_first exists_ */ export function exists(predicate: Predicate): (ma: Option) => boolean { return (ma) => (isNone(ma) ? false : predicate(ma.value)) } /** * Returns `true` if the predicate is satisfied by the wrapped value */ export function exists_(ma: Option, predicate: Predicate): boolean { return isNone(ma) ? false : predicate(ma.value) } /** * Apply `Option[A] => B` in case self is some returning `Option[B]` * * @ets_data_first extend_ */ export function extend(f: (fa: Option) => B) { return (self: Option): Option => extend_(self, f) } /** * Apply `Option[A] => B` in case self is some returning `Option[B]` */ export function extend_(self: Option, f: (fa: Option) => B): Option { return isNone(self) ? none : some(f(self)) } /** * Takes a default value, a function, and an `Option` value, if the `Option` value is `None` the default value is * returned, otherwise the function is applied to the value inside the `Some` and the result is returned. * * @ets_data_first fold_ */ export function fold( onNone: () => B, onSome: (a: A) => C ): (ma: Option) => B | C { return (ma) => fold_(ma, onNone, onSome) } /** * Takes a default value, a function, and an `Option` value, if the `Option` value is `None` the default value is * returned, otherwise the function is applied to the value inside the `Some` and the result is returned. */ export function fold_( ma: Option, onNone: () => B, onSome: (a: A) => C ): B | C { return isNone(ma) ? onNone() : onSome(ma.value) } /** * Constructs `Option[A]` from `Either[E, A]` discarding `E` */ export function fromEither(ma: Either): Option { return ma._tag === "Left" ? none : some(ma.right) } /** * Constructs a new `Option` from a nullable type. If the value is `null` or `undefined`, returns `None`, otherwise * returns the value wrapped in a `Some` */ export function fromNullable(a: A): Option> { return a == null ? none : some(a as NonNullable) } /** * Returns a smart constructor based on the given predicate * * @ets_data_first fromPredicate_ */ export function fromPredicate( refinement: Refinement ): (a: A) => Option /** * Returns a smart constructor based on the given predicate * * @ets_data_first fromPredicate_ */ export function fromPredicate(predicate: Predicate): (a: A) => Option /** * Returns a smart constructor based on the given predicate * * @ets_data_first fromPredicate_ */ export function fromPredicate(predicate: Predicate): (a: A) => Option { return (a) => (predicate(a) ? some(a) : none) } /** * Returns a smart constructor based on the given predicate */ export function fromPredicate_( a: A, refinement: Refinement ): Option /** * Returns a smart constructor based on the given predicate */ export function fromPredicate_(a: A, predicate: Predicate): Option /** * Returns a smart constructor based on the given predicate */ export function fromPredicate_(a: A, predicate: Predicate): Option { return predicate(a) ? some(a) : none } /** * Returns an `E` value if possible */ export function getLeft(ma: Either): Option { return ma._tag === "Right" ? none : some(ma.left) } /** * Extracts the value out of the structure, if it exists. Otherwise returns the given default value * * @ets_data_first getOrElse_ */ export function getOrElse(onNone: () => B): (ma: Option) => A | B { return (o) => getOrElse_(o, onNone) } /** * Extracts the value out of the structure, if it exists. Otherwise returns the given default value * * @ets_data_first getOrElseS_ */ export function getOrElseS(onNone: () => B): (ma: Option) => B { return getOrElse(onNone) } /** * Extracts the value out of the structure, if it exists. Otherwise returns the given default value */ export function getOrElse_(ma: Option, onNone: () => B): A | B { return ma._tag === "None" ? onNone() : ma.value } /** * Extracts the value out of the structure, if it exists. Otherwise returns the given default value */ export function getOrElseS_(ma: Option, onNone: () => A): A { return getOrElse_(ma, onNone) } /** * Returns a `Refinement` (i.e. a custom type guard) from a `Option` returning function. * This function ensures that a custom type guard definition is type-safe. */ export function getRefinement( getOption: (a: A) => Option ): Refinement { return (a: A): a is B => isSome(getOption(a)) } /** * Returns an `A` value if possible */ export function getRight(ma: Either): Option { return ma._tag === "Left" ? none : some(ma.right) } /** * Returns `true` if the option is `None`, `false` otherwise */ export function isNone(fa: Option): fa is None { return fa._tag === "None" } /** * Returns `true` if the option is an instance of `Some`, `false` otherwise */ export function isSome(fa: Option): fa is Some { return fa._tag === "Some" } /** * Use `A => B` to transform `Option[A]` to `Option[B]` */ export function map_(ma: Option, f: (a: A) => B): Option { return isNone(ma) ? none : some(f(ma.value)) } /** * Use `A => B` to transform `Option[A]` to `Option[B]` * * @ets_data_first map_ */ export function map(f: (a: A) => B) { return (fa: Option): Option => map_(fa, f) } /** * This is `chain` + `fromNullable`, useful when working with optional values */ export function mapNullable( f: (a: A) => B | null | undefined ): (ma: Option) => Option { return (ma) => (isNone(ma) ? none : fromNullable(f(ma.value))) } /** * Extracts the value out of the structure, if it exists. Otherwise returns `null`. */ export function toNullable(ma: Option): A | null { return isNone(ma) ? null : ma.value } /** * Extracts the value out of the structure, if it exists. Otherwise returns `undefined`. */ export function toUndefined(ma: Option): A | undefined { return isNone(ma) ? undefined : ma.value } /** * Transforms an exception into an `Option`. If `f` throws, returns `None`, otherwise returns the output wrapped in * `Some` */ export function tryCatch(f: Lazy): Option { try { return some(f()) } catch (e) { return none } } export const PartialExceptionTypeId = Symbol() export type PartialExceptionTypeId = typeof PartialExceptionTypeId export class PartialException { readonly _typeId: PartialExceptionTypeId = PartialExceptionTypeId } function raisePartial(): X { throw new PartialException() } /** * Simulates a partial function */ export function partial( f: (miss: () => X) => (...args: ARGS) => A ): (...args: ARGS) => Option { return (...args) => { try { return some(f(raisePartial)(...args)) } catch (e) { if (e instanceof PartialException) { return none } throw e } } }