Charming Python #19 (w-50039)

Even More Functional Programming in Python

Earlier installments of this columns touched on many basic
concepts of functional programming (FP). This column
continues the discussion by illustrating additional
capabilities, especially those contained in Xoltar Toolkit:
Currying, higher-order functions, and other specialized
concepts.

What Is Python?

Python is a freely available, very-high-level, interpreted
language developed by Guido van Rossum. It combines a clear
syntax with powerful (but optional) object-oriented semantics.
Python is available for almost every computer platform you
might find yourself working on, and has strong portability
between platforms.

Expression Bindings

Never content with partial solutions, one reader--Richard
Davies--raised the issue of whether we might move bindings all
the way into individual expressions. Let's take a quick look
at why we might want to do that, and also show a remarkably
elegant way of expressing this that a comp.lang.python
contributor provided.

Let us first recall the Bindings class of the functional
module. Using the attributes of that class, we were able to
assure that a particular name means only one thing within a
given block scope:

The Bindings class does what we want within a module or
function def scope, but there is no way to make it work
within a single expression. In ML-family languages, however,
it is natural to create bindings within a single expression:

Python 2.0+ expression-level name bindings

This trick of putting an expession inside a single-item tuple
in a list-comprehension does not provide any way of using
expression-level bindings with higher-order functions. To use
the higher-order functions, we still need to use block-level
bindings, as with:

Python block-level bindings with 'map()'

Not bad, but if we want to use map(), the scope of the
binding remains a little broader than we might want.
Nonetheless, it is possible to coax list comprehensions into
doing our name bindings for us, even in cases where a list is
not what we finally want:

What we have done is peform an arithmetic calculation on the
first element of the first element of list_of_list while also
naming the arithmetic calculation (but only in expression
scope). As an "optimization" we might not bother to create a
list longer than one element to start with, since we choose
only the first element with the ending index 0:

Efficient stepping down from list-comprehension

>>> [func for x in list_of_list[:1]
... for car in (x[0],)
... for func in (car+car**2,)][0]
2

Higher-order Functions: Currying

Three of the most general higher-order functions are built into
Python: map(), reduce() and filter(). What these
functions do--and the reason we call them "higher-order"--is
take other functions as (some of) their arguments. Other
higher-order functions, but not these builtins, return function
objects.

Python has always given users the ability to construct their
own higher-order functions by virtue of the first-class status
of function objects. A trivial case might look like:

Trivial Python function factory

The Xoltar Toolkit (see Resources), which I discussed also in
early installments of this column, comes with a nice collection
of higher-order functions. Most of the functions that Xoltar's
functional module provides are ones that have been developed
in various traditionally functional languages, and whose
usefulness has proved itself over many years.

Possibly the most famous and most important higher-order
function is traditionally called curry(). curry() is named
after the logician Haskell Curry, whose first-name is also used
to name the abovementioned programming language. The insight
that underlies "currying" is that it is possible to treat
(almost) every function as a partial function of just one
argument. All that is necessary for currying to work is to
allow the return value of functions to themselves be functions,
but with the returned functions "narrowed" or "closer to
completion." The way this works is quite similar to the
closures that I wrote about in an earlier column--each
successive call to a curried return function "fills in" more of
the data involved in a final computation (data attached to a
procedure).

Let's illustrate currying first with a very simple example in
Haskell, then with the same example repeated in Python using
the functional module:

Unlike with closures, we need to curry the arguments in a
specific order (left to right). But note that functional
also contains an rcurry() class that will start at the other
end (right to left).

The second print statement in the example at one level is a
trivial spelling change from simply calling the normal
taxcalc(50000,0.30,10000). But in a different level it makes
rather clear the concept that every function can be a function
of just one argument--a rather surprising idea to those new to
it.

Miscellanous Higher-order Functions

Beyond the "fundamental" operation of currying, functional
provides a grab-bag of interesting higher-order functions.
Moreover, it is really not hard to write your own higher-order
functions--either with or without functional. The ones in
functional provide some interesting ideas, at the least.

For the most part, higher-order functions feel like "enhanced"
versions of the standard map(), filter() and reduce(). A
lot of the time the pattern in these functions is roughly "take
a function or functions and some lists as arguments, then apply
the function(s) to list arguments." There are a surprising
number of interesting and useful ways to play on this theme.
Another pattern is "take a collection of functions and create a
function that combines their functionality." Again, numerous
variations are possible. Let us look at some of what
functional provides.

The functions seqential() and also() both create a function
based on a sequence of component functions. The component
functions can then be called with the same argument(s). The
main difference between the two is simply that sequential()
expects a single list as an argument, while also() takes a
list of arguments. In most cases, these are useful for
function side effects, but sequential() optionally lets you
choose which function provides the combined return value:

The functions disjoin() and conjoin() are similar to
sequential() and also() in terms of creating new functions
that apply argument(s) to several component functions. But
disjoin() asks whether any component functions return true
(given the argument(s)), and conjoin() asks whether all
components return true. Logical shortcutting is applied, where
possible, so some side effects might not occur with
disjoin(). joinfuncs() is similar to also(), but returns
a tuple of the components' return values rather than selecting
a main one.

Where the previous functions let you call multiple functions
with the same argument(s), any(), all() and none_of() let
you call the same function against a list of arguments. In
general structure, these are a bit like the builtin map(),
reduce(), filter() functions. But these particular
higher-order functions from functional ask boolean questions
about collections of return values. For example:

A particularly interesting higher-order function for those with
a little bit of mathematics background is compose(). The
composition of several functions is a "chaining together" of
the return value of one function to the input of the next
function. The programmer who composes several functions is
responsible for making sure the outputs and inputs match
up--but then, that is true any time a programmer uses a return
value. A simple example makes it clear:

Until Next Time

I hope this latest look at higher-order functions will picque
readers' interest in a certain style of thinking. By all
means, play with it. Try to create some of your own
higher-order functions; some might well prove useful and
powerful. Let me know how it goes, perhaps a later installment
of this ad hoc series will discuss the novel and fascinating
ideas that readers continue to provide.

Resources

The earlier installments of this series on functional
programming in Python can be found at:

Bryn Keller's "xoltar toolkit" which includes the module
functional adds a large number of useful FP extensions to
Python. Since the functional module is itself written
entirely in Python, what it does was already possible in Python
itself. But Keller has figured out a very nicely integrated
set of extensions, with a lot of power in compact definitions.
The toolkit can be found at:

The author has found it much easier to get a grasp of
functional programming via the language Haskell than in
Lisp/Scheme (even though the latter is probably more widely
used, if only in Emacs). Other Python programmers might
similarly have an easier time without quite so many parentheses
and prefix (Polish) operators.

A book with a somewhat more applied feel which is an equally
introduction to Haskell is:

The Haskell School of Expression: Learning Functional
Programming Through Multimeia, Paul Hudak, Cambridge
University Press (2000).

About The Author

Since conceptions without intuitions are empty, and intuitions
without conceptions, blind, David Mertz wants a cast sculpture
of Milton for his office. Start planning for his birthday.
David may be reached at mertz@gnosis.cx; his life pored over at
http://gnosis.cx/publish/. Suggestions and recommendations on
this, past, or future, columns are welcomed.