Guest User

Untitled

a guest
Oct 21st, 2019
71
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (main)
  2.  
  3. Where
  4.  
  5. Define (main)
  6. (option_monad.flat_map
  7. ; (option_sequence.sequence ['some.1 & 'none & 'some.2 & 'nil])
  8. (option_sequence.sequence ['some.1 & 'some.2 & 'nil])
  9. Func numbers
  10. Let {}
  11. (LIST.for_each numbers
  12. Func n [STDIO.printf <- "%d\n" <- n])
  13. In
  14. 'none)
  15.  
  16. Where
  17.  
  18. Let option_sequence (SEQUENCE option_monad)
  19.  
  20. Where
  21.  
  22. Let option_monad
  23. Define (map x f)
  24. Match x {
  25. | 'some.value 'some.(f value)
  26. | 'none 'none
  27. }
  28. Define (flatten x)
  29. Match x {
  30. | 'some.inner inner
  31. | 'none 'none
  32. }
  33. Define (pure value)
  34. 'some.value
  35. Define (flat_map x f)
  36. Match x {
  37. | 'none 'none
  38. | 'some.value (f value)
  39. }
  40. In
  41. {
  42. :map
  43. :flatten
  44. :pure
  45. :flat_map
  46. }
  47.  
  48. Where
  49.  
  50. Define (SEQUENCE MONAD)
  51. Define (sequence actions)
  52. Unfold actions
  53. Match actions {
  54. | 'nil (MONAD.pure 'nil)
  55. | 'cons.{action actions}
  56. (MONAD.flat_map action
  57. Func item
  58. (MONAD.map (Fold actions)
  59. Func items [item & items]))
  60. }
  61. In
  62. {
  63. :MONAD
  64. :sequence
  65. }
  66.  
  67. Where
  68.  
  69. Open Package "list" {:Infix &}
  70.  
  71. Open Package "func" {:Infix <-}
  72.  
  73. Where
  74.  
  75. Let LIST Package "list"
  76. Let STDIO Package "stdio"
RAW Paste Data