Skip to content

Instantly share code, notes, and snippets.

@venil7
Last active May 7, 2019 11:17
Show Gist options
  • Save venil7/6c711e4ccee306baa7fd3b1983a56a60 to your computer and use it in GitHub Desktop.
Save venil7/6c711e4ccee306baa7fd3b1983a56a60 to your computer and use it in GitHub Desktop.
simple maybe functor
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