Applicative

An Applicative is a Functor that implements a method named of:

class Container<T> {
    public static of<TVal>(val: TVal) { 
        return new Container(val); 
    } 
    private _value!: T; 
    public constructor(val: T) { 
        this._value = val; 
    } 
    public map<TMap>(fn: (val: T) => TMap) { 
        return new Container<TMap>(fn(this._value)); 
    } 
} 

We can use the Applicative as follows:

let double = (x: number) => x + x; 
let container = Container.of(3); 
let container2 = container.map(double); 
console.log(container2); // { _value: 6 } 
Please note that the entire example is included in the companion source code.

Get Learning TypeScript 2.x - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.