0 added
0 removed
Original
2026-01-01
Modified
2026-02-21
1
<p>In this lesson, we will look at three examples of<strong>higher-order functions</strong>. Programmers use these functions in many languages. It is worth noting up front that the examples in this lesson are somewhat simplified. We will discuss the Python versions of these functions.</p>
1
<p>In this lesson, we will look at three examples of<strong>higher-order functions</strong>. Programmers use these functions in many languages. It is worth noting up front that the examples in this lesson are somewhat simplified. We will discuss the Python versions of these functions.</p>
2
<p>For greater flexibility and performance, we implement them slightly differently. Simple examples can successfully demonstrate the purpose and general principle of operation.</p>
2
<p>For greater flexibility and performance, we implement them slightly differently. Simple examples can successfully demonstrate the purpose and general principle of operation.</p>
3
<h2>The map function</h2>
3
<h2>The map function</h2>
4
<p>When working with lists, we often have to apply some form of transformation (specifically, the same kind of transformation) for each element. Of course, we can always write a loop. However, this loop will look identical in almost all cases:</p>
4
<p>When working with lists, we often have to apply some form of transformation (specifically, the same kind of transformation) for each element. Of course, we can always write a loop. However, this loop will look identical in almost all cases:</p>
5
<p>In such cases, only the applied f function changes. So why not generalize this code so that the function is a parameter? That is what we are going to do:</p>
5
<p>In such cases, only the applied f function changes. So why not generalize this code so that the function is a parameter? That is what we are going to do:</p>
6
<p>The function is called<strong>map</strong>. The name comes from mathematics. We give the same name to functions that map one set of values to another by converting all elements using some transformation. Most languages use the same name.</p>
6
<p>The function is called<strong>map</strong>. The name comes from mathematics. We give the same name to functions that map one set of values to another by converting all elements using some transformation. Most languages use the same name.</p>
7
<h2>The filter function</h2>
7
<h2>The filter function</h2>
8
<p>Often you do not need to transform the elements so much as you need to keep some of them in the list and discard others according to some criterion. A filter function solves this problem in many languages. The code for this function looks similar to the map code:</p>
8
<p>Often you do not need to transform the elements so much as you need to keep some of them in the list and discard others according to some criterion. A filter function solves this problem in many languages. The code for this function looks similar to the map code:</p>
9
<p>Our filter function applies a predicate to each element and adds elements to the output list only if the predicate returns True. The filter function has a more descriptive name but is just as popular. Many languages have a similar function with the same name.</p>
9
<p>Our filter function applies a predicate to each element and adds elements to the output list only if the predicate returns True. The filter function has a more descriptive name but is just as popular. Many languages have a similar function with the same name.</p>
10
<h2>The reduce function</h2>
10
<h2>The reduce function</h2>
11
<p>Both map and filter work on single elements independently. But there are also loops that aggregate the result, forming a single result argument by combining elements using an accumulator argument.</p>
11
<p>Both map and filter work on single elements independently. But there are also loops that aggregate the result, forming a single result argument by combining elements using an accumulator argument.</p>
12
<p>A typical example of aggregation might be the sum of all items in a list. Or, say, a work. Suppose we want to add all the items in the list:</p>
12
<p>A typical example of aggregation might be the sum of all items in a list. Or, say, a work. Suppose we want to add all the items in the list:</p>
13
<p>Mathematically, the sum looks like that:</p>
13
<p>Mathematically, the sum looks like that:</p>
14
<p>We can express it like this:</p>
14
<p>We can express it like this:</p>
15
<p>Zero here is the same accumulator, the initial state. It does not add anything to the total, but it can serve as a starting point. And it will serve as the result if the input list is empty. If we use a loop, we would add things up like this:</p>
15
<p>Zero here is the same accumulator, the initial state. It does not add anything to the total, but it can serve as a starting point. And it will serve as the result if the input list is empty. If we use a loop, we would add things up like this:</p>
16
<p>And multiply them like this:</p>
16
<p>And multiply them like this:</p>
17
<p>Notice a trend? The loops differ only in:</p>
17
<p>Notice a trend? The loops differ only in:</p>
18
<ul><li>The initial value of the accumulator (0 and 1)</li>
18
<ul><li>The initial value of the accumulator (0 and 1)</li>
19
<li>The operation that combines the element and accumulator (+and *)</li>
19
<li>The operation that combines the element and accumulator (+and *)</li>
20
</ul><p>Let us summarize:</p>
20
</ul><p>Let us summarize:</p>
21
<p>In the example, we used the add and mul from the operator module. These are equivalents of + and * that we can use in conjunction with higher-order functions. The reduce function is not as lucky with its name as the previous two. We could call this function inject, reduce, and aggregate.</p>
21
<p>In the example, we used the add and mul from the operator module. These are equivalents of + and * that we can use in conjunction with higher-order functions. The reduce function is not as lucky with its name as the previous two. We could call this function inject, reduce, and aggregate.</p>
22
<p>Only in mathematics is everything unambiguous. This kind of function is<strong>left fold</strong>. And the name is telling: when we use this function, we fold the list into one value. Our fold is left because we start folding the elements with the accumulation on the left. There is also a<strong>right fold</strong>, but it is not a built-in function in Python.</p>
22
<p>Only in mathematics is everything unambiguous. This kind of function is<strong>left fold</strong>. And the name is telling: when we use this function, we fold the list into one value. Our fold is left because we start folding the elements with the accumulation on the left. There is also a<strong>right fold</strong>, but it is not a built-in function in Python.</p>
23
<p>The right fold for the sum looks like this:</p>
23
<p>The right fold for the sum looks like this:</p>
24
<p>In most cases, both folds give the same result if the applied operation is<strong>associative</strong>- it allows you to place brackets however you want, as is the case for sums. But if you iterate through elements using a loop, it is easier to implement the left fold, which we use more often.</p>
24
<p>In most cases, both folds give the same result if the applied operation is<strong>associative</strong>- it allows you to place brackets however you want, as is the case for sums. But if you iterate through elements using a loop, it is easier to implement the left fold, which we use more often.</p>
25
<h2>The higher-order functions versus for loops</h2>
25
<h2>The higher-order functions versus for loops</h2>
26
<p>We have implemented three full functions with less power than the for loop. In addition, the for loop allows you to flexibly manage the iteration process using the break and continue commands.</p>
26
<p>We have implemented three full functions with less power than the for loop. In addition, the for loop allows you to flexibly manage the iteration process using the break and continue commands.</p>
27
<p>So why do we need these separate, weaker versions when there is a universal one? These functions do one job, which makes thinking, reading, and writing code much more straightforward.</p>
27
<p>So why do we need these separate, weaker versions when there is a universal one? These functions do one job, which makes thinking, reading, and writing code much more straightforward.</p>
28
<p>As soon as we look at the function, we can understand that filter filters and map maps. Moreover, filter does not change the elements because of how we do it, just discarding some of them. And map changes the value of the items but does not change their number or position. Knowing what the code cannot do is worth a lot.</p>
28
<p>As soon as we look at the function, we can understand that filter filters and map maps. Moreover, filter does not change the elements because of how we do it, just discarding some of them. And map changes the value of the items but does not change their number or position. Knowing what the code cannot do is worth a lot.</p>
29
<p>If we had a for loop, we would have to execute the code we have to interpret because the for loop can do everything:</p>
29
<p>If we had a for loop, we would have to execute the code we have to interpret because the for loop can do everything:</p>
30
<ul><li>Change elements</li>
30
<ul><li>Change elements</li>
31
<li>Discard them</li>
31
<li>Discard them</li>
32
<li>Aggregate the result</li>
32
<li>Aggregate the result</li>
33
<li>Do everything simultaneously</li>
33
<li>Do everything simultaneously</li>
34
</ul><p>Of course, we cannot replace every use of the for loop with a function. But in those simple cases where it is possible to achieve the desired result using a less powerful and easier-to-understand tool, it is worth at least thinking about this alternative.</p>
34
</ul><p>Of course, we cannot replace every use of the for loop with a function. But in those simple cases where it is possible to achieve the desired result using a less powerful and easier-to-understand tool, it is worth at least thinking about this alternative.</p>