Skip to content

Instantly share code, notes, and snippets.

@Xion
Created March 23, 2012 15:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xion/2171998 to your computer and use it in GitHub Desktop.
Save Xion/2171998 to your computer and use it in GitHub Desktop.
Monads in Python!
# Monadic IO
def print_(s):
def _():
print s
return _
def some_io():
for x in ['Hello', 'World']:
yield print_(x)
# Runner
def run_monad(m):
try:
x = next(m)
while True:
x = m.send(x())
except StopIteration:
pass
>>> run_monad(some_io())
Hello
World
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment