Currying the hard way

We can create curried functions manually, without using the decorator from the PyMonad library; one way of doing this is shown in the function definition that follows:

def f(x, *args):
def f1(y, *args):
def f2(z):
return (x+y)*z
if args:
return f2(*args)
return f2
if args:
return f1(*args)
return f1

This curries a function, , into a function, f(x), which returns a function. Conceptually, . We then curried the intermediate function to create the f1(y) and f2(z) function, .

When we evaluate the f(x) function, we'll get a new function, f1, as a result. If additional arguments are provided, those arguments are passed to the f1 function for evaluation, either resulting in a final value or another function.

Clearly, this kind of manual expansion to implement currying is potentially error-prone. This isn't a practical approach to working with functions. It  may, however, serve to illustrate what currying means and how it's implemented in Python. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset