Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. // Maybe[Nothing, Just]
  2. class Maybe {
  3. constructor(maybe) { this.maybe = maybe }
  4.  
  5. static of(value) {
  6. if (this.isNothing()) {
  7. return new Nothing
  8. }
  9. return Just.of(this.value)
  10. }
  11.  
  12. isNothing = () => !!this.maybe
  13. }
  14.  
  15. // Just
  16. class Just {
  17. construcor(value) { this.value = value }
  18.  
  19. static of = value => new Just(value)
  20.  
  21. map = f => Just.of(f(this.value))
  22.  
  23. bind = f => this.map(f).join()
  24.  
  25. join = () => this.value
  26.  
  27. isJust = () => true
  28.  
  29. isNothing = () => false
  30.  
  31. orSome = val => this.value
  32. }
  33.  
  34. // Nothing
  35. class Nothing {
  36. construcor(value) {}
  37.  
  38. static of = value => new Nothing
  39.  
  40. map = f => this
  41.  
  42. bind = f => this
  43.  
  44. isJust = () => false
  45.  
  46. isNothing = () => true
  47.  
  48. orSome = val => val
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement