Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Function1Static

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.

Hierarchy

  • Function1Static

Index

Methods

constant

  • The constant function of one parameter: will always return the value you give, no matter the parameter it's given.

    Type parameters

    • U

    • T

    Parameters

    • val: T

    Returns Function1<U, T>

id

liftEither

  • 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")
    

    Type parameters

    • T

    • L

    • U

    Parameters

    • fn: function
        • (x: T): U
        • Parameters

          • x: T

          Returns U

    • Optional witness: L

    Returns Function1<T, Either<L, U>>

liftNullable

  • 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()
    

    Type parameters

    • T

    • U

    Parameters

    • fn: function
        • (x: T): U | null | undefined
        • Parameters

          • x: T

          Returns U | null | undefined

    Returns Function1<T, Option<U>>

liftOption

  • 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()
    

    Type parameters

    • T

    • U

    Parameters

    • fn: function
        • (x: T): U | undefined
        • Parameters

          • x: T

          Returns U | undefined

    Returns Function1<T, Option<U>>

of

Generated using TypeDoc