Functional Python Programming

(Wang) #1

The PyMonad Library


Currying the hard way


We can create curried functions manually, without using the decorator from the
pymonad library; one way of doing this is to execute the following commands:


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, Fx(),,yz →+()xy×z, into a function, f(x), which returns a
function. Conceptually, fx()→Fy′(),z. 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 is potentially error-prone. It does, however, serve to define what
currying really means and how it's implemented in Python.


Functional composition and the PyMonad multiplication operator


One of the significant values of curried functions is the ability to combine
them via functional composition. We looked at functional composition in
Chapter 5, Higher-order Functions, and Chapter 11, Decorator Design Techniques.


When we've created a curried function, we can easily perform function composition
to create a new, more complex curried function. In this case, the PyMonad package
defines the * operator for composing two functions. To show how this works, we'll
define two curried functions that we can compose. First, we'll define a function that
computes the product, and then we'll define a function that computes a specialized
range of values.

Free download pdf