Functional Python Programming

(Wang) #1
Chapter 5

Given the statistics, we used the outlier lambda to filter our data. If the normalized
value is too large, the data is an outlier.


The result of list(filter(outlier, trip)) is a list of two legs that are quite long
compared to the rest of the legs in the population. The average distance is about
34 nm, with a standard deviation of 24 nm. No trip can have a normalized distance
of less than -1.407.


We're able to decompose a fairly complex problem into a number
of independent functions, each one of which can be easily tested in
isolation. Our processing is a composition of simpler functions. This
can lead to succinct, expressive functional programming.

The iter() function with a sentinel value


The built-in iter() function creates an iterator over a collection object. We can use
this to wrap an iterator object around a collection. In many cases, we'll allow the
for statement to handle this implicitly. In a few cases, we might want to create an
iterator explicitly so that we can separate the head from the tail of a collection. This
function can also iterate through the values created by a callable or function until a
sentinel value is found. This feature is sometimes used with the read() function
of a file to consume rows until some sentinel value is found. In this case, the given
function might be some file's readline() method. Providing a callable function to
iter() is a bit hard for us because this function must maintain state internally. This
hidden state is a feature of an open file, for example, each read() or readline()
function advances some internal state to the next character or next line.


Another example of this is the way that a mutable collection object's pop()
method makes a stateful change in the object. The following is an example
of using the pop() method:





tail= iter([1, 2, 3, None, 4, 5, 6].pop, None)








list(tail)





[6, 5, 4]


The tail variable was set to an iterator over the list [1, 2, 3, None, 4, 5, 6]
that will be traversed by the pop() function. The default behavior of pop() is pop
(-1), that is, the elements are popped in the reverse order. When the sentinel
value is found, the iterator stops returning values.


This kind of internal state is something we'd like to avoid as much as possible.
Consequently, we won't try to contrive a use for this feature.

Free download pdf