Way back in the 0th Episode, Steve talked about functional programming languages, specifically Scheme. Curious, I checked out Tom’s favorite resource to learn more. What do you know, but there’s a Python example. And I thought I would have to change languages in order to parallelize better. Chalk one up for PHP/Python/Ruby, Dr. V.
In the spirit of infecting other people with the Python bug, let me explain the Wikipedia example a little. There are some tricks in Python that seem a little cryptic at times, and six months ago this would have been one of them for me. For those of you still learning Python (or those of you sticking to Curly Brackets instead of adopting the Whitespace Rules), here’s an explanation of what’s going on.
Here’s the example Wikipedia gives. Suppose you want to do something like apply two functions/methods/routines to every element in a list. For those of you who are doing math-related functions, let’s call those functions F and G, and let’s call the list x.If you’re math-aware, you might recognize the notation F(G(x)). In other words, we’re going to apply the function G to every element in x and get a new list back. Then we’re going to apply the function F to every element in the new list and get an even newer list back.
If you wanted to do this the standard way, it would work like this (in Python):
result = [] # This is a new empty list.
for value in x: # Loop through the elements in the list x.
first_result = G(value) # Get the G(x)
second_result = F(first_result) # Get F(G(x))
result.append(second_result) # Add the result to the list of results.
While this works, it could be more succinct. There are two pythonic ways to do this. One uses syntactic sugar (list comprehensions), and one uses something a bit more complex that does it the way function programming languages do. Since this post is about functional programming, we’ll focus on that one.
composition = lambda F, G: lambda x: F(G(x)) result = map(composition(F, G), x)
That’s it. If you know something about lambda functions in Python, it’s not too bad. If you’re not familiar with lambda functions, let me try to clear things up a bit.
A lambda function is an anonymous function that only has one line of code and returns something. For instance, I could write,
def func(x):
return 2*x
or, using a lambda function, I could write,
func = lambda x: 2*x
Lambda functions let the programmer define all sorts of simple functions without cluttering up the code with function definitions. They don’t even have to be stored as a variable.
To demonstrate, let’s skip ahead a little and look at the map function. map takes two arguments: a function, and a list (or tuple). map takes each element from the list, runs it through the function, and returns a list containing all the results. For instance,
doubled_values = map(func, x)
This creates a list called doubled_values. The map function takes each element in x and runs it through the function func (remember the function definitions above). Since func returns two times the input value, each element in doubled_values is two times the corresponding element in x.
Now that you know how the map function works, let’s rewrite it simpler. If you only need func this one time, it doesn’t make a lot of sense to have that lingering function definition in the code. Instead, you can write it like this:
doubled_values = map(lambda a: 2*a, x)
This defines a function with a parameter a that returns 2*a. That becomes the function that map runs every value of x through in order to produce doubled_values.
Let’s take another look at our functional programming example.
composition = lambda F, G: lambda x: F(G(x)) result = map(composition(F, G), x)
This creates the variable composition and stores in it a function in terms of F and G. composition(F, G) is in turn a function of x. Thus, if you were going to call composition for some specific value of x, say 2, you would write
composition(F,G)(2)
This works because composition(F,G) is a function, and functions can be called.
This makes the second line of our example a little clearer. map is passing each element of x through the function composition(F,G) and storing the outputs in result.
The trickiest part of this is understanding the lambda functions. The great thing is that lambda functions can easily be experimented with in Python’s interactive mode. Just open up the interactive interpreter and play around with it. Here is an example of what you can do based on this tutorial.
>>> x = [1,2,3,4,5] >>> F = lambda x: 2*x >>> G = lambda x: x**2 >>> composition = lambda A, B: lambda x: A(B(x)) >>> result = map(composition(F,G), x) >>> print(result) [2, 8, 18, 32, 50]

