Skip to content

Instantly share code, notes, and snippets.

@ccorcos
Last active March 2, 2017 06:33
Show Gist options
  • Save ccorcos/0e42f1ea0cf9a24c306319ee7355391c to your computer and use it in GitHub Desktop.
Save ccorcos/0e42f1ea0cf9a24c306319ee7355391c to your computer and use it in GitHub Desktop.
class Result {
constructor(value, rest) {
this.value = value
this.rest = rest
}
}
class Success extends Result {
map(fn) {
return new Success(fn(this.value), this.rest)
}
bimap(s, f) {
return new Success(s(this.value), this.rest)
}
chain(fn) {
return fn(this.value, this.rest)
}
fold(s, f) {
return s(this.value, this.rest)
}
}
class Failure extends Result {
map(fn) {
return this
}
bimap(s, f) {
return new Failure(f(this.value), this.rest)
}
chain(fn) {
return this
}
fold(s, f) {
return f(this.value, this.rest)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment