import { Either, isLeft } from './Either' import { Arity1, curry } from '@typed/lambda' import { fromLeft } from './Left' import { fromRight } from './Right' /** * Extracts the value from an `Either` applying function `f` if the `Either` is * `Left` or function `g` if `Right`. * @name unpack(f: Arity1, g: Arity1, either: Either): C */ export const unpack = curry(__unpack) as { (f: Arity1, g: Arity1, either: Either): C (f: Arity1, g: Arity1): (either: Either) => C (f: Arity1): UnpackArity2A (f: Arity1): UnpackArity2B } export type UnpackArity2A = { (g: Arity1, either: Either): C (g: Arity1): (either: Either) => C } export type UnpackArity2B = { (g: Arity1, either: Either): C (g: Arity1): (either: Either) => C } function __unpack(f: (value: A) => C, g: (value: B) => C, either: Either): C { return isLeft(either) ? f(fromLeft(either)) : g(fromRight(either)) }