O'Reilly logo

Functional Programming: A PragPub Anthology by Michael Swaine

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required