a = yield monadic_function(var1. var2)
monadic_function(var1, var2) >> (lambda a: ...)
yield ignore_this_result()
to ignore_this_result >> (lambda _: ... )
Monad.unit(final)
final = yield monadic_func_n(c)
c = yield monadic_func_c(a, b)
b = yield monadic_func_b(...)
a = yield monadic_func_a(...)
thing = ...
In [1]:
import ast
import inspect
from pynads import Just, Nothing, Maybe
from pynads.utils.decorators import kwargs_decorator
#dummy mreturn and do
@kwargs_decorator
def do(f, monad):
return f
mreturn = lambda x: x
In [2]:
def safe_div(a,b):
if b == 0:
return Nothing
else:
return Just(a/b)
@do(monad=Maybe)
def safe_div_do(first):
a = yield safe_div(1, first)
b = yield safe_div(first, 3)
c = yield safe_div(b, a)
mreturn(c)
def safe_div_bind(first):
return safe_div(1, first) >> (lambda a:
safe_div(first, 3) >> (lambda b:
safe_div(a, b) >> (lambda c:
Maybe.unit(c) )))
In [3]:
safe_div_do(1)
Out[3]:
In [4]:
safe_div_do_ast = ast.parse(inspect.getsource(inspect.unwrap(safe_div_do)))
safe_div_bind_ast = ast.parse(inspect.getsource(safe_div_bind))
# replacement indicator
# note: this applied *after* we parsed the ast
safe_div_do.original = True
In [5]:
safe_div_do_ast.body[0].body = safe_div_bind_ast.body[0].body
In [6]:
f = compile(safe_div_do_ast, '<string>', 'exec')
exec(f, globals(), locals())
In [7]:
safe_div_do(1)
Out[7]: