Curried type guard for Either Sometimes needed also due to https://github.com/Microsoft/TypeScript/issues/20218
Vector.of(Either.right<number,number>(2), Either.left<number,number>(1))
.filter(Either.isLeft)
.map(o => o.getLeft())
=> Vector.of(1)
Curried type guard for Either Sometimes needed also due to https://github.com/Microsoft/TypeScript/issues/20218
Vector.of(Either.right<number,number>(2), Either.left<number,number>(1))
.filter(Either.isRight)
.map(o => o.get())
=> Vector.of(2)
Constructs an Either containing a left value which you give.
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")
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")
the first right type
the second right type
the left type
the new right type as returned by the combining 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)
the left type
the object property type specifying the parameters for your function
the type returned by your function, returned wrapped in an either by liftAp.
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))
the left type
the object property type specifying the parameters for your function
the type returned by your function, returned wrapped in an either by liftAp.
Constructs an Either containing a right value which you give.
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
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))
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
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
Generated using TypeDoc
Holds the "static methods" for Either