Options
All
  • Public
  • Public/Protected
  • All
Menu

Class EitherStatic

Holds the "static methods" for Either

Hierarchy

  • EitherStatic

Index

Methods

isLeft

  • isLeft<L, R>(e: Either<L, R>): boolean

isRight

  • isRight<L, R>(e: Either<L, R>): boolean

left

  • left<L, R>(val: L): Either<L, R>
  • Constructs an Either containing a left value which you give.

    Type parameters

    • L

    • R

    Parameters

    • val: L

    Returns Either<L, R>

lift

  • lift<T, L, U>(fn: function, witness?: L): function
  • Take a partial function (may return undefined or throw), and lift it to return an Either instead.

    Note that unlike the OptionStatic.lift version, if the function returns undefined, the Either.lift version will throw (the Option.lift version returns None()): if you want to do pure side-effects which may throw, you're better off just using javascript try blocks.

    When using typescript, to help the compiler infer the left type, you can either pass a second parameter like {} as <type>, or call with lift<L,R>(...).

    const add = Either.lift((x:number,y:number) => x+y, {} as string);
    add(1,2);
    => Either.right(3)
    
    const undef = Either.lift((x:number,y:number,z:number) => undefined);
    undef(1,2,3);
    => throws
    
    const throws = Either.lift(() => {throw "x"});
    throws();
    => Either.left("x")
    

    Type parameters

    • T: any[]

    • L

    • U

    Parameters

    • fn: function
        • (...args: T): U
        • Parameters

          • Rest ...args: T

          Returns U

    • Optional witness: L

    Returns function

      • Parameters

        • Rest ...args: T

        Returns Either<L, U>

liftA2

  • liftA2<R1, R2, L, V>(fn: function, leftWitness?: L): function
  • Applicative lifting for Either. Takes a function which operates on basic values, and turns it in a function that operates on eithers of these values ('lifts' the function). The 2 is because it works on functions taking two parameters.

    const lifted = Either.liftA2(
        (x:number,y:number) => x+y, {} as string);
    lifted(
        Either.right<string,number>(5),
        Either.right<string,number>(6));
    => Either.right(11)
    
    const lifted = Either.liftA2(
        (x:number,y:number) => x+y, {} as string);
    lifted(
        Either.right<string,number>(5),
        Either.left<string,number>("bad"));
    => Either.left("bad")
    

    Type parameters

    • R1

      the first right type

    • R2

      the second right type

    • L

      the left type

    • V

      the new right type as returned by the combining function.

    Parameters

    • fn: function
        • (v1: R1, v2: R2): V
        • Parameters

          • v1: R1
          • v2: R2

          Returns V

    • Optional leftWitness: L

    Returns function

liftAp

  • liftAp<L, A, B>(fn: function, leftWitness?: L): function
  • Applicative lifting for Either. 'p' stands for 'properties'.

    Takes a function which operates on a simple JS object, and turns it in a function that operates on the same JS object type except which each field wrapped in an Either ('lifts' the function). It's an alternative to EitherStatic.liftA2 when the number of parameters is not two.

    const fn = (x:{a:number,b:number,c:number}) => x.a+x.b+x.c;
    const lifted = Either.liftAp(fn, {} as number);
    lifted({
        a: Either.right<number,number>(5),
        b: Either.right<number,number>(6),
        c: Either.right<number,number>(3)});
    => Either.right(14)
    
    const lifted = Either.liftAp<number,{a:number,b:number},number>(
        x => x.a+x.b);
    lifted({
        a: Either.right<number,number>(5),
        b: Either.left<number,number>(2)});
    => Either.left(2)
    

    Type parameters

    • L

      the left type

    • A

      the object property type specifying the parameters for your function

    • B

      the type returned by your function, returned wrapped in an either by liftAp.

    Parameters

    • fn: function
        • (x: A): B
        • Parameters

          • x: A

          Returns B

    • Optional leftWitness: L

    Returns function

      • Parameters

        • x: object

        Returns Either<L, B>

liftApAcc

  • liftApAcc<L, A, B>(fn: function, leftWitness?: L): function
  • Applicative lifting for Either. 'p' stands for 'properties'. Compared to EitherStatic.liftAp, liftApAcc 'accumulates' the errors, instead of short-circuiting on the first error.

    Takes a function which operates on a simple JS object, and turns it in a function that operates on the same JS object type except which each field wrapped in an Either ('lifts' the function). It's an alternative to EitherStatic.liftA2 when the number of parameters is not two.

    const fn = (x:{a:number,b:number,c:number}) => x.a+x.b+x.c;
    const lifted = Either.liftApAcc(fn, {} as number);
    lifted({
        a: Either.right<number,number>(5),
        b: Either.right<number,number>(6),
        c:Either.right<number,number>(3)});
    => Either.right(14)
    
    const fn = (x:{a:number,b:number,c:number}) => x.a+x.b+x.c;
    const lifted = Either.liftApAcc(fn, {} as number);
    lifted({
        a: Either.right<number,number>(5),
        b: Either.left<number,number>(2),
        c: Either.left<number,number>(6)});
    => Either.left(Vector.of(2, 6))
    

    Type parameters

    • L

      the left type

    • A

      the object property type specifying the parameters for your function

    • B

      the type returned by your function, returned wrapped in an either by liftAp.

    Parameters

    • fn: function
        • (x: A): B
        • Parameters

          • x: A

          Returns B

    • Optional leftWitness: L

    Returns function

right

  • right<L, R>(val: R): Either<L, R>
  • Constructs an Either containing a right value which you give.

    Type parameters

    • L

    • R

    Parameters

    • val: R

    Returns Either<L, R>

sequence

  • Turns a list of eithers in an either containing a list of items. Useful in many contexts.

    Either.sequence(Vector.of(
        Either.right<number,number>(1),
        Either.right<number,number>(2)));
    => Either.right(Vector.of(1,2))
    

    But if a single element is Left, everything is discarded:

    Either.sequence(Vector.of(
          Either.right<number,number>(1),
          Either.left<number,number>(2),
          Either.left<number,number>(3)));
    => Either.left(2)
    

    Also see EitherStatic.traverse

    Type parameters

    • L

    • R

    Parameters

    • elts: Iterable<Either<L, R>>

    Returns Either<L, Vector<R>>

sequenceAcc

  • Turns a list of eithers in an either containing a list of items. Compared to EitherStatic.sequence, sequenceAcc 'accumulates' the errors, instead of short-circuiting on the first error.

    Either.sequenceAcc(Vector.of(
        Either.right<number,number>(1),
        Either.right<number,number>(2)));
    => Either.right(Vector.of(1,2))
    

    But if a single element is Left, you get all the lefts:

    Either.sequenceAcc(Vector.of(
          Either.right<number,number>(1),
          Either.left<number,number>(2),
          Either.left<number,number>(3)));
    => Either.left(Vector.of(2,3))
    

    Type parameters

    • L

    • R

    Parameters

    • elts: Iterable<Either<L, R>>

    Returns Either<Vector<L>, Vector<R>>

traverse

  • traverse<T, L, R>(elts: Iterable<T>, fn: function): Either<L, Vector<R>>
  • Takes a list, a function that can transform list elements to eithers, then return an either containing a list of the transformed elements.

    const getUserById: (x:number)=>Either<string,string> = x => x > 0 ?
        Either.right("user" + x.toString()) : Either.left("invalid id!");
    Either.traverse([4, 3, 2], getUserById);
    => Either.right(Vector.of("user4", "user3", "user2"))
    

    But if a single element results in Left, everything is discarded:

    const getUserById: (x:number)=>Either<string,string> = x => x > 0 ?
        Either.right("user" + x.toString()) : Either.left("invalid id!");
    Either.traverse([4, -3, 2], getUserById);
    => Either.left("invalid id!")
    

    Also see EitherStatic.sequence

    Type parameters

    • T

    • L

    • R

    Parameters

    • elts: Iterable<T>
    • fn: function

    Returns Either<L, Vector<R>>

try_

  • try_<L, T>(fn: function, witness?: L): Either<L, T>
  • Take a no-parameter partial function (may return undefined or throw), call it, and return an Either instead.

    Note that unlike the OptionStatic.try_ version, if the function returns undefined, this function will throw (the Option.try_ version returns None()): if you want to do pure side-effects which may throw, you're better off just using javascript try blocks.

    When using typescript, to help the compiler infer the left type, you can either pass a second parameter like {} as <type>, or call with try_<L,R>(...).

    Either.try_(Math.random, {} as string);
    => Either.right(0.49884723907769635)
    
    Either.try_(() => undefined);
    => throws
    
    Either.try_(() => {throw "x"});
    => Either.left("x")
    

    Also see EitherStatic.lift, OptionStatic.try_, OptionStatic.tryNullable

    Type parameters

    • L

    • T

    Parameters

    • fn: function
        • (): T
        • Returns T

    • Optional witness: L

    Returns Either<L, T>

Generated using TypeDoc