Higher-order Functions
A very important feature of the functional programming paradigm is higher-order
functions. These are functions that accept functions as arguments or return functions
as results. Python offers several of these kinds of functions. We'll look at them and
some logical extensions.
As we can see, there are three varieties of higher-order functions, which are
as follows:
- Functions that accept a function as one of its arguments
- Functions that return a function
- Functions that accept a function and return a function
Python offers several higher-order functions of the first variety. We'll look at these
built-in higher-order functions in this chapter. We'll look at a few of the library
modules that offer higher-order functions in later chapters.
The idea of a function that emits functions can seem a bit odd. However, when we
look at a Callable class object, we see a function that returns a Callable object. This is
one example of a function that creates another function.
Functions that accept functions and create functions include complex Callable classes
as well as function decorators. We'll introduce decorators in this chapter, but defer
deeper consideration of decorators until Chapter 11, Decorator Design Techniques.
Sometimes we wish that Python had higher-order versions of the collection functions
from the previous chapter. In this chapter, we'll show the reduce(extract())
design pattern to perform a reduction on specific fields extracted from a larger tuple.
We'll also look at defining our own version of these common collection-processing
functions.