Functional Python Programming

(Wang) #1

Recursions and Reductions


This allows us to define mean, standard deviation, normalized values, correction,
and even least-squares linear regression using a few simple functions.


The last of our simple reductions, s2(), shows how we can apply existing
reductions to create higher-order functions. We might change our approach
to be more like the following:


def sum_f(function, data):


return sum(function(x) for x in data)


We've added a function that we'll use to transform the data. We'll compute the sum
of the transformed values.


Now we can apply this function in three different ways to compute the three
essential sums as follows:


N= sum_f(lambda x: 1, data) # x**0


S= sum_f(lambda x: x, data) # x**1


S2= sum_f( lambda x: x*x, data ) # x**2


We've plugged in a small lambda to compute ∑∑xX∈ ()x^0 = xX∈^1 , which is the count,

∑∑xX∈ ()xx^1 = xX∈ , the sum, and

2
∑xX∈ x , the sum of the squares, which we can use
to compute standard deviation.


A common extension to this includes a filter to reject raw data which is unknown
or unsuitable in some way. We might use the following command to reject bad data:


def sum_filter_f(filter, function, data):


return sum(function(x) for x in data if filter(x))


Execution of the following command snippet allows us to do things like reject None
values in a simple way:


count_= lambda x: 1


sum_ = lambda x: x


valid = lambda x: x is not None


N = sum_filterf(valid, count, data)


This shows how we can provide two distinct lambda to our sum_filter_f() function.
The filter argument is a lambda that rejects None values, we've called it valid to
emphasize its meaning. The function argument is a lambda that implements a count
or a sum method. We can easily add a lambda to compute a sum of squares.

Free download pdf