Created
March 23, 2012 15:47
-
-
Save Xion/2171998 to your computer and use it in GitHub Desktop.
Monads in Python!
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
# 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