Lesson 9. Higher-order functions
After reading lesson 9, you’ll be able to
- Understand higher-order functions
- Use map, filter, and foldl to avoid writing explicitly recursive functions
- Implement many higher-order functions yourself
In the preceding lesson, you saw a wide variety of recursive functions. Although practice makes writing recursive code easier, many functions share the exact same patterns of recursion. Therefore, you can abstract out this recursion into a small number of commonly used functions that don’t require you to think about recursion explicitly. The practical answer to the challenge of writing recursive code is that you usually use these existing functions, which are part of a group of functions referred to as higher-order functions.
A higher-order function is technically any function that takes another function as an argument. Typically, when higher-order functions are mentioned, a specific group of them comes to mind, and nearly all of these are used to abstract away common patterns of recursion. In this lesson, you’ll look at higher-order functions that make writing recursive functions much easier. The true cure for recursive headaches is abstraction!