5.9 Review of some design patterns
The max()
, min()
, and sorted()
functions have a default behavior without a key=
function. They can be customized by providing a function that defines how to compute a key from the available data. For many of our examples, the key=
function has been a simple extraction of available data. This isn’t a requirement; the key=
function can do anything.
Imagine the following method: max(trip,
key=random.randint())
. Generally, we try not to have key=
functions that do something obscure like this.
The use of a key=
function is a common design pattern. Functions we design can easily follow this pattern.
We’ve also looked at the way lambda forms can simplify the application of higher-order functions. One significant advantage of using lambda forms is that it follows the functional paradigm very closely. When writing more conventional functions, we can create imperative programs that might clutter an otherwise succinct and expressive functional...