Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Left<L, R>

Represents an Either containing a left value, conceptually tied to a failure. "static methods" available through EitherStatic

Type parameters

  • L

    the "left" item type 'failure'

  • R

    the "right" item type 'success'

Hierarchy

  • Left

Implements

  • any

Index

Constructors

constructor

  • new Left(value: L): Left

Methods

__computed

  • __computed(): string
  • Used by the node REPL to display values.

    Returns string

bimap

  • bimap<S, T>(fnL: function, fnR: function): Either<S, T>
  • Map the either: you give a function to apply to the value, a function in case it's a left, a function in case it's a right.

    Type parameters

    • S

    • T

    Parameters

    • fnL: function
        • (x: L): S
        • Parameters

          • x: L

          Returns S

    • fnR: function
        • (x: R): T
        • Parameters

          • x: R

          Returns T

    Returns Either<S, T>

contains

  • Returns true if this is either is a right and contains the value you give.

    Parameters

    Returns boolean

equals

  • Two objects are equal if they represent the same value, regardless of whether they are the same object physically in memory.

    Parameters

    Returns boolean

filter

  • filter(p: function, filterVal: function): Either<L, R>
  • "filter" the either. If it was a Left, it stays a Left. If it was a Right and the predicate you pass returns true for its value, return the either unchanged. But if it was a left and the predicate returns false, return a Left with the value returned by the function passed as second parameter.

    Either.right<string,number>(-3)
        .filter(x => x >= 0, v => "got negative value: " + v);
    => Either.left<string,number>("got negative value: -3")
    

    Parameters

    • p: function
        • (x: R): boolean
        • Parameters

          • x: R

          Returns boolean

    • filterVal: function
        • (x: R): L
        • Parameters

          • x: R

          Returns L

    Returns Either<L, R>

flatMap

  • flatMap<U>(fn: function): Either<L, U>
  • If this either is a right, call the function you give with the contents, and return what the function returns, else returns this. This is the monadic bind.

    Type parameters

    • U

    Parameters

    • fn: function

    Returns Either<L, U>

getLeft

  • getLeft(): L
  • Get the value contained in this left. NOTE: we know it's there, since this method belongs to Left, not Either.

    Returns L

getLeftOrElse

  • getLeftOrElse(other: L): L
  • If this either is a left, return its value, else return the value you give.

    Parameters

    • other: L

    Returns L

getLeftOrThrow

  • getLeftOrThrow(message?: undefined | string): L
  • If this either is a left, return its value, else throw an exception. You can optionally pass a message that'll be used as the exception message.

    Parameters

    • Optional message: undefined | string

    Returns L

getOrElse

  • getOrElse(other: R): R
  • If this either is a right, return its value, else return the value you give.

    Parameters

    • other: R

    Returns R

getOrThrow

  • getOrThrow(errorInfo?: Error | string): R
  • If this either is a right, return its value, else throw an exception. You can optionally pass a message that'll be used as the exception message, or an Error object.

    Parameters

    • Optional errorInfo: Error | string

    Returns R

hasTrueEquality

  • hasTrueEquality(): boolean

hashCode

  • hashCode(): number
  • Get a number for that object. Two different values may get the same number, but one value must always get the same number. The formula can impact performance.

    Returns number

ifLeft

  • ifLeft(fn: function): Either<L, R>
  • Execute a side-effecting function if the either is a left; returns the either.

    Parameters

    • fn: function
        • (x: L): void
        • Parameters

          • x: L

          Returns void

    Returns Either<L, R>

ifRight

  • ifRight(fn: function): Either<L, R>
  • Execute a side-effecting function if the either is a right; returns the either.

    Parameters

    • fn: function
        • (x: R): void
        • Parameters

          • x: R

          Returns void

    Returns Either<L, R>

isLeft

  • isLeft(): boolean
  • Returns true since this is a Left

    Returns boolean

isRight

  • isRight(): boolean
  • Returns false since this is a Left

    Returns boolean

map

  • map<U>(fn: function): Either<L, U>
  • If this either is a right, applies the function you give to its contents and build a new right either, otherwise return this.

    Type parameters

    • U

    Parameters

    • fn: function
        • (x: R): U
        • Parameters

          • x: R

          Returns U

    Returns Either<L, U>

mapLeft

  • mapLeft<U>(fn: function): Either<U, R>
  • If this either is a left, call the function you give with the left value and return a new either left with the result of the function, else return this.

    Type parameters

    • U

    Parameters

    • fn: function
        • (x: L): U
        • Parameters

          • x: L

          Returns U

    Returns Either<U, R>

match

  • match<U>(cases: object): U
  • Handle both branches of the either and return a value (can also be used for side-effects). This is the catamorphism for either.

    Either.right<string,number>(5).match({
        Left:  x => "left " + x,
        Right: x => "right " + x
    });
    => "right 5"
    

    Type parameters

    • U

    Parameters

    • cases: object

    Returns U

orElse

  • Combines two eithers. If this either is a right, returns it. If it's a left, returns the other one.

    Parameters

    Returns Either<L, R>

recoverWith

  • recoverWith(recoveryFn: function): Either<L, R>
  • Has no effect if this Either is a right. If it's a left however, the function you give will be called, receiving as parameter the left contents, and an Either equivalent to the one your function returns will be returned.

    Parameters

    • recoveryFn: function
        • Parameters

          • left: L

          Returns Either<L, R>

    Returns Either<L, R>

toLinkedList

  • Convert to a list. If it's a left, it's the empty list, if it's a right, it's a one-element list with the contents of the either.

    Returns LinkedList<R>

toOption

  • Convert this either to an option, conceptually dropping the left (failing) value.

    Returns Option<R>

toString

  • toString(): string
  • Get a human-friendly string representation of that value.

    Returns string

toVector

  • Convert to a vector. If it's a left, it's the empty vector, if it's a right, it's a one-element vector with the contents of the either.

    Returns Vector<R>

transform

  • transform<U>(converter: function): U
  • Transform this value to another value type. Enables fluent-style programming by chaining calls.

    Type parameters

    • U

    Parameters

    • converter: function

    Returns U

Generated using TypeDoc