Functional Python Programming

(Wang) #1

Working with Collections


In the next chapter, we'll focus on using higher-order collection functions to do
similar kinds of processing.


An overview of function varieties


We need to distinguish between two broad species of functions, as follows:



  • Scalar functions apply to individual values and compute an individual result.
    Functions such as abs(), pow(), and the entire math module are examples of
    scalar functions.

  • Collection() functions work with iterable collections.


We can further subdivide the collection functions into three subspecies:



  • Reduction: This uses a function that is used to fold values in the collection
    together, resulting in a single final value. We can call this an aggregate
    function, as it produces a single aggregate value for an input collection.

  • Mapping: This applies a function to all items of a collection; the result is
    a collection of the same size.

  • Filter: This applies a function to all items of a collection that rejects some
    items and passes others. The result is a subset of the input. A filter might do
    nothing, which means that the output matches the input; this is an improper
    subset, but it still fits the broader definition of subset.


We'll use this conceptual framework to characterize ways in which we use the
built-in collection functions.


Working with iterables


As we noted in the previous chapters, we'll often use Python's for loop to work
with collections. When working with materialized collections such as tuples, lists,
maps, and sets, the for loop involves an explicit management of state. While this
strays from purely functional programming, it reflects a necessary optimization for
Python. If we assure that state management is localized to an iterator object that's
created as part of the for statement evaluation, we can leverage this feature without
straying too far from pure, functional programming. For example, if we use the for
loop variable outside the indented body of loop, we've strayed too far from purely
functional programming.


We'll return to this in Chapter 6, Recursion and Reduction. It's an important topic, and
we'll just scratch the surface here with a quick example of working with generators.

Free download pdf