Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Maybe[Nothing, Just]
- class Maybe {
- constructor(maybe) { this.maybe = maybe }
- static of(value) {
- if (this.isNothing()) {
- return new Nothing
- }
- return Just.of(this.value)
- }
- isNothing = () => !!this.maybe
- }
- // Just
- class Just {
- construcor(value) { this.value = value }
- static of = value => new Just(value)
- map = f => Just.of(f(this.value))
- bind = f => this.map(f).join()
- join = () => this.value
- isJust = () => true
- isNothing = () => false
- orSome = val => this.value
- }
- // Nothing
- class Nothing {
- construcor(value) {}
- static of = value => new Nothing
- map = f => this
- bind = f => this
- isJust = () => false
- isNothing = () => true
- orSome = val => val
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement