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