Skip to content

Instantly share code, notes, and snippets.

@Sgeo
Created June 12, 2013 00:32
Show Gist options
  • Save Sgeo/5762049 to your computer and use it in GitHub Desktop.
Save Sgeo/5762049 to your computer and use it in GitHub Desktop.
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