(no title)
elclanrs | 11 years ago
class Maybe {
constructor(value) {
if (value != null) {
return new Just(value)
}
return new Nothing()
}
bind(f) {
if (this instanceof Just) {
return f(this.value)
}
return new Nothing()
}
}
class Just extends Maybe {
constructor(value) {
this.value = value
}
toString() {
return `<Just ${this.value}>`
}
}
class Nothing extends Maybe {
constructor() {
this.value = null
}
toString() {
return '<Nothing>'
}
}
var result = Maybe(2).bind(x => {
return Maybe(3).bind(y => {
return Maybe(x + y)
})
})
console.log(result.toString()) // <Just 5>
No comments yet.