Last active
May 7, 2019 11:17
-
-
Save venil7/6c711e4ccee306baa7fd3b1983a56a60 to your computer and use it in GitHub Desktop.
simple maybe functor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Maybe<T> { | |
constructor(private val: T | null|undefined) { } | |
public get hasVal():boolean { | |
return (this.val !== null || this.val !== undefined); | |
} | |
public value(fallback: T):T { | |
return this.hasVal ? this.val : fallback; | |
} | |
public map<U>(func:(t: T) => U): Maybe<U> { | |
return this.hasVal | |
? new Maybe(func(this.val)) | |
: new Maybe<U>(null); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment