The constant function of two parameters: will always return the value you give, no matter the parameters it's given.
Take a two-parameter partial function (may return undefined or throw), and lift it to return an Either instead.
Note that unlike the Function2Static.liftOption version, if the function returns undefined, the liftEither version will throw (the liftOption 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>(...)
.
const add = Function2.liftEither((x:number,y:number) => x+y, {} as string);
add(1,2);
=> Either.right(3)
const undef = Function2.liftEither((x:number,y:number) => undefined);
undef(1,2);
=> throws
const throws = Function2.liftEither((x:number,y:number) => {throw "x"});
throws(1,2);
=> Either.left("x")
Take a two-parameter partial function (may return undefined or throw), and lift it to return an Option instead. null and undefined become a None, everything else a Some
const plus = Function2.liftNullable((x:number,y:number)=>x+y);
plus(1,2);
=> Option.of(3)
const undef = Function2.liftNullable((x:number,y:number)=>undefined);
undef(1,2);
=> Option.none()
const nl = Function2.liftNullable((x:number,y:number)=>null);
nl(1,2);
=> Option.none()
const throws = Function2.liftNullable((x:number,y:number)=>{throw "x"});
throws(1,2);
=> Option.none()
Take a two-parameter partial function (may return undefined or throw), and lift it to return an Option instead. undefined becomes a None, everything else a Some
const plus = Function2.liftOption((x:number,y:number)=>x+y);
plus(1,2);
=> Option.of(3)
const undef = Function2.liftOption((x:number,y:number)=>undefined);
undef(1,2);
=> Option.none()
const nl = Function2.liftOption((x:number,y:number)=>null);
nl(1,2);
=> Option.some(null)
const throws = Function2.liftOption((x:number,y:number)=>{throw "x"});
throws(1,2);
=> Option.none()
Take a two-parameter function and lift it to become a Function2, enabling you to call Function2.andThen and other such methods on it.
Generated using TypeDoc
This is the type of the Function2 constant, which offers some helper functions to deal with Function2 including the ability to build Function2 from functions using Function2Static.of. It also offers some builtin functions like Function2Static.constant.