Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Function2Static

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.

Hierarchy

  • Function2Static

Index

Methods

constant

  • constant<T1, T2, R>(val: R): Function2<T1, T2, R>
  • The constant function of two parameters: will always return the value you give, no matter the parameters it's given.

    Type parameters

    • T1

    • T2

    • R

    Parameters

    • val: R

    Returns Function2<T1, T2, R>

liftEither

  • liftEither<T1, T2, L, R>(fn: function, witness?: L): Function2<T1, T2, Either<L, R>>
  • 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")
    

    Type parameters

    • T1

    • T2

    • L

    • R

    Parameters

    • fn: function
        • (x: T1, y: T2): R
        • Parameters

          • x: T1
          • y: T2

          Returns R

    • Optional witness: L

    Returns Function2<T1, T2, Either<L, R>>

liftNullable

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

    Type parameters

    • T1

    • T2

    • R

    Parameters

    • fn: function
        • (x: T1, y: T2): R | null | undefined
        • Parameters

          • x: T1
          • y: T2

          Returns R | null | undefined

    Returns Function2<T1, T2, Option<R>>

liftOption

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

    Type parameters

    • T1

    • T2

    • R

    Parameters

    • fn: function
        • (x: T1, y: T2): R | undefined
        • Parameters

          • x: T1
          • y: T2

          Returns R | undefined

    Returns Function2<T1, T2, Option<R>>

of

  • of<T1, T2, R>(fn: function): Function2<T1, T2, R>
  • Take a two-parameter function and lift it to become a Function2, enabling you to call Function2.andThen and other such methods on it.

    Type parameters

    • T1

    • T2

    • R

    Parameters

    • fn: function
        • (x: T1, y: T2): R
        • Parameters

          • x: T1
          • y: T2

          Returns R

    Returns Function2<T1, T2, R>

Generated using TypeDoc