import * as Arr from './Arr'; import * as Fun from './Fun'; import { Optional } from './Optional'; /** * **Is** the value stored inside this Optional object equal to `rhs`? */ export const is = (lhs: Optional, rhs: T, comparator: (a: T, b: T) => boolean = Fun.tripleEquals): boolean => lhs.exists((left) => comparator(left, rhs)); /** * Are these two Optional objects equal? Equality here means either they're both * `Some` (and the values are equal under the comparator) or they're both `None`. */ export const equals: { (lhs: Optional, rhs: Optional, comparator: (a: A, b: B) => boolean): boolean; (lhs: Optional, rhs: Optional): boolean; } = (lhs: Optional, rhs: Optional, comparator: (a: A, b: B) => boolean = Fun.tripleEquals as any): boolean => lift2(lhs, rhs, comparator).getOr(lhs.isNone() && rhs.isNone()); export const cat = (arr: Optional[]): A[] => { const r: A[] = []; const push = (x: A) => { r.push(x); }; for (let i = 0; i < arr.length; i++) { arr[i].each(push); } return r; }; export const sequence = (arr: ArrayLike>): Optional> => { const r: A[] = []; for (let i = 0; i < arr.length; i++) { const x = arr[i]; if (x.isSome()) { r.push(x.getOrDie()); } else { return Optional.none>(); } } return Optional.some(r); }; /** @deprecated Use Arr.findMap instead. */ export const findMap = Arr.findMap; /** Map each element of an array to an Optional and collect the results. * If all results are "some", return Optional.some of the results. * If any result is "none", return Optional.none */ export const traverse = (arr: ArrayLike, f: (a: A) => Optional): Optional> => sequence(Arr.map(arr, f)); /* Notes on the lift functions: - We used to have a generic liftN, but we were concerned about its type-safety, and the below variants were faster in microbenchmarks. - The getOrDie calls are partial functions, but are checked beforehand. This is faster and more convenient (but less safe) than folds. - && is used instead of a loop for simplicity and performance. */ export const lift2 = (oa: Optional, ob: Optional, f: (a: A, b: B) => C): Optional => oa.isSome() && ob.isSome() ? Optional.some(f(oa.getOrDie(), ob.getOrDie())) : Optional.none(); export const lift3 = (oa: Optional, ob: Optional, oc: Optional, f: (a: A, b: B, c: C) => D): Optional => oa.isSome() && ob.isSome() && oc.isSome() ? Optional.some(f(oa.getOrDie(), ob.getOrDie(), oc.getOrDie())) : Optional.none(); export const lift4 = (oa: Optional, ob: Optional, oc: Optional, od: Optional, f: (a: A, b: B, c: C, d: D) => E): Optional => oa.isSome() && ob.isSome() && oc.isSome() && od.isSome() ? Optional.some(f(oa.getOrDie(), ob.getOrDie(), oc.getOrDie(), od.getOrDie())) : Optional.none(); export const lift5 = (oa: Optional, ob: Optional, oc: Optional, od: Optional, oe: Optional, f: (a: A, b: B, c: C, d: D, e: E) => F): Optional => oa.isSome() && ob.isSome() && oc.isSome() && od.isSome() && oe.isSome() ? Optional.some(f(oa.getOrDie(), ob.getOrDie(), oc.getOrDie(), od.getOrDie(), oe.getOrDie())) : Optional.none(); export const mapFrom = (a: A | null | undefined, f: (a: A) => B): Optional => (a !== undefined && a !== null) ? Optional.some(f(a)) : Optional.none(); export const bindFrom = (a: A | null | undefined, f: (a: A) => Optional): Optional => (a !== undefined && a !== null) ? f(a) : Optional.none(); export const flatten = (oot: Optional>): Optional => oot.bind(Fun.identity); // This can help with type inference, by specifying the type param on the none case, so the caller doesn't have to. export const someIf = (b: boolean, a: A): Optional => b ? Optional.some(a) : Optional.none();