import { Lazy } from './function'; import { Alt2 } from './Functors/Alt'; import { ChainRec2 } from './Functors/ChainRec'; import { Comonad2 } from './Functors/Comonad'; import { Monad2 } from './Functors/Monad'; import { PipeableTraverse2, Traversable2 } from './Functors/Traversable'; import { Maybe } from './Maybe'; import { Predicate } from './Predicate'; import { Refinement } from './Refinement'; export interface Left { readonly _tag: 'Left'; readonly left: E; } export interface Right { readonly _tag: 'Right'; readonly right: A; } export declare type Either = Left | Right; export declare const EitherKind: unique symbol; export declare type EitherKind = typeof EitherKind; declare module './Functors/HKT' { interface Kinded { readonly [EitherKind]: Either; } } /** * Constructs a new `Either` holding a `Left` value. Represents a failure value. */ export declare const left: (e: E) => Either; /** * Constructs a new `Either` holding a `Right` value. Represents a successful value. */ export declare const right: (a: A) => Either; /** * Returns whether the `Either` is `Left` or not. * * ```ts * assert.deepStrictEqual(isLeft(left(0)), true) * assert.deepStrictEqual(isLeft(right(1)), false) * ``` */ export declare const isLeft: (ma: Either) => ma is Left; /** * Returns whether the `Either` is `Right` or not. * * ```ts * assert.deepStrictEqual(isRight(right(1)), true) * assert.deepStrictEqual(isRight(left(0)), false) * ``` */ export declare const isRight: (ma: Either) => ma is Right; /** * Alias of `left`. */ export declare const zero: (e: E) => Either; /** * Alias of `left`. */ export declare const empty: (e: E) => Either; /** * Maps the `Right` value. */ export declare const map: (f: (a: A) => B) => (ma: Either) => Left | Right; /** * Takes a value and wraps it into a `Right`. */ export declare const of: (a: A) => Either; /** * Returns the `Either` if it is `Right`, otherwise returns the function result. */ export declare const alt: (that: Lazy>) => (ma: Either) => Either; /** * Applies a `Right` function over a `Right` value. Returns `Left` if the `Either` or the function is `Left`. */ export declare const ap: (ma: Either) => (fab: Either B>) => Either; /** * Constructs a `Left` or `Right` based on the given predicate. * * @example * * ```ts * const getEither = fromPredicate((n: number) => n > 0, () => 'error') * * assert.deepStrictEqual(getEither(1), right(1)) * assert.deepStrictEqual(getEither(-1), left('error')) * ``` */ export declare function fromPredicate(refinement: Refinement, onFalse: Lazy): (a: A) => Either; export declare function fromPredicate(predicate: Predicate, onFalse: Lazy): (b: B) => Either; export declare function fromPredicate(predicate: Predicate, onFalse: Lazy): (a: A) => Either; /** * Returns `Left` or `Right` based on the given `Maybe`. * * @example * * ```ts * const f = flow( * fromMaybe(() => 'error') * ) * * assert.deepStrictEqual(f(some(1)), right(1)) * assert.deepStrictEqual(f(none), left('error')) * ``` */ export declare const fromMaybe: (onNone: Lazy) => (ma: Maybe) => Left | Right; /** * Takes two functions and an `Either` value, if the value is `Left`, * returns the `onLeft` function result, if the value is `Right`, returns the `onRight` function result. * * @example * * ```ts * const f = match((n: number) => n - 1, (n: number) => n + 1) * * assert.deepStrictEqual(f(left(1)), 0) * assert.deepStrictEqual(f(right(1)), 2) * ``` */ export declare const match: (onLeft: (e: E) => B, onRight: (a: A) => C) => (ma: Either) => B | C; /** * Returns the `Either` value if it's a `Right` or a default `onLeft` result value if it's a `Left`. * * @example * * ```ts * assert.deepStrictEqual(getOrElse(() => 0)(left(1)), 0) * assert.deepStrictEqual(getOrElse(() => 0)(right(1)), 1) * ``` */ export declare const getOrElse: (onLeft: (e: E) => B) => (ma: Either) => B | A; /** * Returns the `Left` value of an `Either` if possible. * * @example * * ```ts * assert.deepStrictEqual(getLeft(right(1)), none) * assert.deepStrictEqual(getLeft(left(1)), some(1)) * ``` */ export declare const getLeft: (ma: Either) => Maybe; /** * Returns the `Right` value of an `Either` if possible. * * @example * * ```ts * assert.deepStrictEqual(getRight(right(1)), some(1)) * assert.deepStrictEqual(getRight(left(1)), none) * ``` */ export declare const getRight: (ma: Either) => Maybe; /** * Extracts the value out of `Either`. * * @example * * ```ts * assert.deepStrictEqual(extract(right(1)), 1) * assert.deepStrictEqual(extract(left('err')), 'err') * ``` */ export declare const extract: (ma: Either) => E | A; /** * Returns the `Either` if it's a `Left`, otherwise returns the result of the applying function and wrapped in a `Right`. * * @example * * ```ts * assert.deepStrictEqual(pipe(right(1), extend(() => 2)), right(2)) * assert.deepStrictEqual(pipe(left(1), extend(() => 2)), left(1)) * ``` */ export declare const extend: (f: (ma: Either) => B) => (ma: Either) => Left | Right; /** * Composes computations in sequence. Useful for chaining many computations that may fail. * * @example * * ```ts * assert.deepStrictEqual(chain((n: number) => right(n + 1))(left(1)), left(1)) * assert.deepStrictEqual(chain((n: number) => right(n + 1))(right(1)), right(2)) * ``` */ export declare const chain: (f: (a: A) => Either) => (ma: Either) => Either; /** * Chains recursively until the next is `Right`. * * @example * * ```ts * assert.deepStrictEqual( * pipe( * right(1), * chainRec( * a => a < 5 ? left(a + 1) : right(`${a}`) * ) * ) * , right('5')) * ``` */ export declare const chainRec: (f: (a: A) => Either>) => (ma: A) => Either; /** * Takes a function and an initial value and returns the initial value if `Either` is `Left`, * otherwise returns the result of applying the function to the initial value and the value inside `Either`. * * @example * * ```ts * assert.deepStrictEqual(pipe(right(1), reduce((acc, a) => acc + a, 1), 2) * assert.deepStrictEqual(pipe(left(0), reduce((acc, a) => acc + a, 1), 1) * ``` */ export declare const reduce: (f: (acc: B, a: A) => B, b: B) => (ma: Either) => B; /** * Maps each element of a `HKT` structure to an action, and collects the results wrapped in `Right`. * * Returns a `HKT` contains a left with the value of `Either` if the `Either` is a `Left`. * * @example * * ```ts * const f = traverse(Maybe.Monad)((n: number) => n > 0 ? some(n): none) * assert.deepStrictEqual(pipe(left('err'), f), some(left('err'))) * assert.deepStrictEqual(pipe(right(1), f), some(right(1))) * assert.deepStrictEqual(pipe(right(-1), f), none) * ``` */ export declare const traverse: PipeableTraverse2; /** * Returns `Either` if it's a `Right`, otherwise returns onLeft result. * * @example * * assert.deepStrictEqual(orElse((n: number) => right(n + 1))(left(1)), right(2)) * assert.deepStrictEqual(orElse((n: number) => right(n + 1))(right(1)), right(1)) */ export declare const orElse: (onLeft: (e: E1) => Either) => (ma: Either) => Either; /** * Returns `false` if `Either` is a `Left`, otherwise returns the predicate result. * * @example * * assert.deepStrictEqual(exists((n: number) => n > 0)(left(0)), false) * assert.deepStrictEqual(exists((n: number) => n > 0)(right(0)), false) * assert.deepStrictEqual(exists((n: number) => n > 0)(right(1)), true) */ export declare const exists: (predicate: Predicate) => (ma: Either) => boolean; /** * Returns a `Either` from a function that might throw. * * @example * * ```ts * const unsafeDiv = (top: number, bottom: number) => { * if(bottom === 0) throw new Error('unsafe division') * return top / bottom * } * const div = (top: number, bottom: number) => * tryCatch(() => unsafeDiv(top, bottom), () => 0) * * assert.deepStrictEqual(div(2, 0), left(0)) * assert.deepStrictEqual(div(2, 1), right(2)) * ``` */ export declare const tryCatch: (f: Lazy, onThrow: (e: unknown) => E) => Either; /** * Returns Right if `Either` is `Left` and vice versa. * * @example * * ```ts * assert.deepStrictEqual(swap(right(1)), left(1)) * assert.deepStrictEqual(swap(left(1)), right(1)) * ``` */ export declare const swap: (ma: Either) => Either; /** * Compares one `Either` to another `Either`. Returns false if eithers or the wrapped values are different. * * @example * * ```ts * assert.deepStrictEqual(equals(right(1), right(1)), true) * assert.deepStrictEqual(equals(right(2), right(1)), true) * assert.deepStrictEqual(equals(right(1), left(1)), false) * assert.deepStrictEqual(equals(left(1), left(1)), true) * assert.deepStrictEqual(equals(left(1), right(1)), false) * ``` */ export declare const equals: (a: Either, b: Either) => boolean; /** * Returns `Either` if it is a `Left` or the result of predicate is true, * otherwise returns the result of applying onFalse function to value inside `Either` and wrapped in a `Left`. * * @example * * ```ts * const f = filterOrElse( * (n: number) => n > 0, * () => 'err' * ) * assert.deepStrictEqual(pipe(right(1), f), right(1)) * assert.deepStrictEqual(pipe(right(-1), f), left('err')) * assert.deepStrictEqual(pipe(left(1), f), left(1)) * ``` */ export declare const filterOrElse: (predicate: Predicate, onFalse: (a: A) => E2) => (ma: Either) => Left | Right | Left; /** * Alt Functor */ export declare const Alt: Alt2; /** * Monad Functor */ export declare const Monad: Monad2; /** * ChainRec Functor */ export declare const ChainRec: ChainRec2; /** * Comonad Functor */ export declare const Comonad: Comonad2; /** * Traversable Functor */ export declare const Traversable: Traversable2;