Created
January 24, 2020 12:47
-
-
Save mykolaharmash/4798bdead4da6f001d8a0a1f702733db to your computer and use it in GitHub Desktop.
This file contains hidden or 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
export class Maybe<T> { | |
value: T | null | |
constructor(value: T | null) { | |
this.value = value | |
} | |
static property<P>(value: P | undefined): Maybe<P> { | |
if (value === undefined) { | |
return new Maybe<P>(null) | |
} | |
return new Maybe<P>(value) | |
} | |
flatMap<M>(fn: (value: T) => Maybe<M>): Maybe<M> { | |
if (this.value === null) { | |
return new Maybe<M>(null) | |
} | |
return fn(this.value) | |
} | |
mapf<M>(fn: (value: T) => M): Maybe<M> { | |
if (this.value === null) { | |
return new Maybe<M>(null) | |
} | |
return new Maybe<M>(fn(this.value)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment