Created
June 12, 2013 00:32
-
-
Save Sgeo/5762049 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
REBOL [ | |
Title: "Monads" | |
Author: "Sgeo" | |
] | |
do-monad: func [ | |
monad "An object containing a /bind and a /return." | |
assigns [block!] | |
result [block!] | |
/local ma f next-assigns arg | |
][ | |
either empty? assigns [ | |
monad/return do result | |
][ | |
arg: first assigns | |
if not set-word? arg [1 / 0] | |
arg: to word! arg | |
ma: do/next next assigns 'next-assigns | |
f: func reduce [arg] reduce [:do-monad monad next-assigns result] | |
monad/bind :ma :f | |
] | |
] | |
identity-m: context [ | |
bind: func [ma f] [f ma] | |
return: func [a] [:a] | |
] | |
list-m: context [ | |
bind: func [ma f [any-function!] /local result] [ | |
result: copy [] | |
foreach a ma [ | |
append result f a | |
] | |
result | |
] | |
return: func [a] [reduce [a]] | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment