In this post, I’d like to provide an overview to built-in higher-order python functions such as
reduce and how you can use them to simplify the overall structure of your code.
Let’s start by defining a simple problem:
Given a list of integers, I want to be able to obtain a list of booleans of whether or not the integers are even.
We can write a traditional solution this way:
The code above works, but it seems too verbose for our liking. In Functional programming, we have the following in our arsenal:
- Immutable data
- First-class functions
- Higher-order functions
- Pure functions
- Recursion, tail recursion
- Iterators, sequences, lazy evaluation, pattern matching, monads
Now, let’s see an alternative solution with the
map higher-order function and
lambda functions (anonymous functions):
map(function, sequence[, sequence, …]) -> list
Return a list of the results of applying the function to the items of the argument sequence(s). If more than one sequence is given, the function is called with an argument list consisting of the corresponding item of each sequence, substituting None for missing values when not all sequences have the same length. If the function is None, return a list of the items of the sequence (or a list of tuples if more than one sequence).
Lambda functions are anonymous functions. What does that mean? They’re functions without a declared name. Let’s look at an example so you can see what I mean. In the above example, we can sort alphabetically without defining the iseven function like so:
As you can see, higher order functions lend to more concise solutions. Let’s take a look at another problem:
Given a list of integers, I want to be able to obtain a list of just the even numbers.
An alternative solution with the
filter higher-order function:
filter(function or None, sequence) -> list, tuple, or string
Return those items of sequence for which function(item) is true. If function is None, return the items that are true. If sequence is a tuple or string, return the same type, else return a list.
Finally, we have
reduce, also known as
accumulate in some languages. This function is an abstraction above the
fold method, if you’re familiar with functional programming.
reduce takes in an iterable and returns one value.
reduce(function, sequence[, initial]) -> value
Apply a function of two arguments cumulatively to the items of a sequence, from left to right, so as to reduce the sequence to a single value. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5). If initial is present, it is placed before the items of the sequence in the calculation, and serves as a default when the sequence is empty.
As you can see, we have a lambda function of two arguments. The left argument
x is the accumulated value and
y is the next iterator value.
Alternatively, you can solve the first problem using list comprehensions:
📬 Get updates straight to your inbox.
Subscribe to my newsletter to make sure you don't miss anything.