January 2019
Beginner
210 pages
4h 47m
English
The following Maybe data type is a Functor and an Applicative, which means that it contains a value and implements the map method. The main difference with the preceding implementation of Functor is that the value contained is optional:
class MayBe<T> { public static of<TVal>(val?: TVal) { return new MayBe(val); } private _value!: T; public constructor(val?: T) { if (val) { this._value = val; } } public isNothing() { return (this._value === null || this._value === undefined); } public map<TMap>(fn: (val: T) => TMap) { if (this.isNothing()) { return new MayBe<TMap>(); } else { return new MayBe<TMap>(fn(this._value)); } } public ap<TMap>(c: MayBe<(val: T) => TMap>) { return c.map(fn => this.map(fn)); }}
As we can see in the preceding ...
Read now
Unlock full access