Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface Either<L, R>

Type parameters

  • L

  • R

Hierarchy

Implemented by

Index

Methods

caseOf

  • caseOf<U>(matcher: CaseOf<L, R, U>): U
  • Returns false if it's Left and true otherwise.

    import { Either } from 'monad-maniac'
    
    const left = Either.left<string, number>('Some Error')
    const right = Either.right<string, number>(150)
    
    const matcher: Either.CaseOf<string, number, number> = {
      Right: (x) => x * 2,
      Left: (message) => message.length,
    }
    
    const resultLeft = left.caseOf(matcher) // 10
    const resultRight = right.caseOf(matcher) // 300
    

    Type parameters

    • U

    Parameters

    Returns U

cata

  • cata<U>(leftFn: (value: L) => U, rightFn: (value: R) => U): U
  • Works like caseOf, but use two functions as arguments for unwrap instead of object allocation.

    Type parameters

    • U

    Parameters

    • leftFn: (value: L) => U

      Function will called with Left

        • (value: L): U
        • Parameters

          • value: L

          Returns U

    • rightFn: (value: R) => U

      Function will called with Right

        • (value: R): U
        • Parameters

          • value: R

          Returns U

    Returns U

chain

  • Apply some function to value in container. map for Right will call the function with value, for Left return Left else value what be returned from the function.

    import { Either } from 'monad-maniac'
    
    const divide = (dividend: number) => (divider: number): Either.Shape<string, number> => {
     if (divider === 0) {
       return Either.left('Divider is zero!')
     }
     return Either.right(dividend / divider)
    }
    
    const nonZeroMultiply = (multiplicand: number) => (factor: number): Either.Shape<string, number> => {
     if (factor === 0) {
       return Either.left('Factor is zero!')
     }
     return Either.right(multiplicand * factor)
    }
    
    const resultNormal = divide(10)(2).chain(nonZeroMultiply(20)).get() // 100
    const resultErrorDivide = divide(10)(0).chain(nonZeroMultiply(20)).get() // 'Divider is zero!'
    const resultErrorMultiply= divide(0)(2).chain(nonZeroMultiply(20)).get() // 'Factor is zero!'
    const resultError = divide(0)(0).chain(nonZeroMultiply(20)).get() // 'Divider is zero!'
    
    

    Type parameters

    • U

    Parameters

    • f: (value: R) => Either<L, U>

      Function to apply for Right value

        • Parameters

          • value: R

          Returns Either<L, U>

    Returns Either<L, U>

filter

  • filter(predicate: (value: R) => boolean): Either<L | R, R>
  • Apply predicate function to value in container. If the function returns not true then value from Right will be transferred to Left.

    import { Either } from 'monad-maniac'
    
    const example = Either.right<number, number>(0)
    const result = example.filter((x) => x !== 0).map((x) => 1 / x).get() // 0
    

    Parameters

    • predicate: (value: R) => boolean
        • (value: R): boolean
        • Parameters

          • value: R

          Returns boolean

    Returns Either<L | R, R>

get

  • get(): L | R
  • Just returns value from Right or Left

    import { Either } from 'monad-maniac'
    
    const left = Either.left<number, number>(300)
    const right = Either.right<number, number>(150)
    
    const resultLeft = left.get() // 300
    const resultRight = right.get() // 150
    

    Returns L | R

getOrElse

  • getOrElse<U>(defaultValue: U): R | U
  • If Either is Left returns defaultValue and returns value from Right otherwise.

    import { Either } from 'monad-maniac'
    
    const left = Either.left<number, number>(150)
    const right = Either.right<number, number>(150)
    
    const resultLeft = left.getOrElse(0) // 0
    const resultRight = right.getOrElse(0) // 150
    

    Type parameters

    • U

    Parameters

    • defaultValue: U

    Returns R | U

isLeft

  • isLeft(): boolean
  • Returns true if it's Left and false otherwise.

    import { Either } from 'monad-maniac'
    
    const left = Either.left<number, number>(300)
    const right = Either.right<number, number>(150)
    
    const resultLeft = left.isLeft() // true
    const resultRight = right.isLeft() // false
    

    Returns boolean

isRight

  • isRight(): boolean
  • Returns false if it's Left and true otherwise.

    import { Either } from 'monad-maniac'
    
    const left = Either.left<number, number>(300)
    const right = Either.right<number, number>(150)
    
    const resultLeft = left.isRight() // false
    const resultRight = right.isRight() // true
    

    Returns boolean

map

  • map<U>(f: (value: R) => U): Either<L, U>
  • Apply some function to value in container. map for Right will call the function with value, for Left return Left. If map will return null or undefined then return Right with that. Be careful.

    import { Either } from 'monad-maniac'
    
    const divide = (dividend: number) => (divider: number): Either.Shape<string, number> => {
    if (divider === 0) {
      return Either.left('Divider is zero!')
    }
     return Either.right(dividend / divider)
    }
    
    const resultNormal = divide(10)(5).map(double).get() // 4
    const resultErrorDivide = divide(10)(0).map(double).get() // 'Divider is zero!'
    

    Type parameters

    • U

    Parameters

    • f: (value: R) => U

      Function to apply for Right value

        • (value: R): U
        • Parameters

          • value: R

          Returns U

    Returns Either<L, U>

or

  • or(f: () => R): Either<L, R>
  • Apply some function to value in container. map for Left. Returns value will be wrap in Right

    import { Either } from 'monad-maniac'
    
    const divide = (dividend: number) => (divider: number): Either.Shape<string, number> => {
    if (divider === 0) {
      return Either.left('Divider is zero!')
    }
     return Either.right(dividend / divider)
    }
    
    const resultErrorDivide = divide(10)(0).map(double).get() // 'Divider is zero!'
    const resultErrorDivide = divide(10)(0).map(double).or(() => 0).get() // 0
    

    Parameters

    • f: () => R

      Function to apply for Right value

        • (): R
        • Returns R

    Returns Either<L, R>

orElse

  • orElse<U>(f: (value: L) => U): R | U
  • Apply some function to value in container Left. Like map for Right.

    The methods returns result from function or value from Right.

    import { Either } from 'monad-maniac'
    
    const left = Either.left<Error, string>(new Error('Some error'))
    const right = Either.right<Error, string>('Jake')
    
    const resultLeft = left.orElse((error) => error.message) // Some error
    const resultRight = right.orElse((error) => error.message) // Jake
    

    Type parameters

    • U

    Parameters

    • f: (value: L) => U

      Function to apply for Right value

        • (value: L): U
        • Parameters

          • value: L

          Returns U

    Returns R | U

toMaybe

  • Converting Either to Maybe. If value in Left will returns Nothing, else returns result Maybe.of for value from Right.

    import { Either } from 'monad-maniac'
    
    const left = Either.left<string, number>('Some Error')
    const right = Either.right<string, number>(150)
    const rightVoid = Either.right<string, number | void>(undefined)
    
    const resultLeft = left.toMaybe() // Nothing()
    const resultRight = right.toMaybe() // Just(150)
    const resultRightVoid = rightVoid.toMaybe() // Nothing()
    

    Returns Shape<R>

toString

  • toString(): string
  • Returns Left or Right as string: Left(left value) or Right(right value).

    import { Either } from 'monad-maniac'
    
    const left = Either.left<number, number>(300)
    const right = Either.right<number, number>(150)
    
    const resultLeft = left.toString() // Left(300)
    const resultRight = right.toString() // Right(150)
    

    Returns string

Generated using TypeDoc