Recursion, Control, and Higher-Order Functions
Pattern matching gives us decision making, but what about loops and so on? Most of the time, we don’t really need them either.
Again, think data and transformations. When we have a collection of things to process in some way—say, a list of numbers that we want to turn into a list of colors, or a tree containing strings for which we want the leaf count—most of the transformations we want fall into two main patterns: mapping and folding.
Mapping is about applying the same operation to everything in a collection, but keeping the shape the same. For example, we can add 2 onto elements in the list [1,2,3] to get [3,4,5]. The order and count of elements stays the same. The operation to perform is ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access