Skip to content

Instantly share code, notes, and snippets.

@mykolaharmash
Created January 24, 2020 12:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mykolaharmash/4798bdead4da6f001d8a0a1f702733db to your computer and use it in GitHub Desktop.
Save mykolaharmash/4798bdead4da6f001d8a0a1f702733db to your computer and use it in GitHub Desktop.
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