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