To become significantly more reliable, code must become more transparent. In particular, nested conditions and loops must be viewed with great suspicion. Complicated control flows confuse programmers. Messy code often hides bugs.
— Bjarne Stroustrup
A functional is a function that takes a function as an input and returns a vector as output. Here’s a simple functional: it calls the function provided as input with 1000 random uniform numbers.
function(f) f(runif(1e3)) randomise <-randomise(mean) #>  0.506 randomise(mean) #>  0.501 randomise(sum) #>  489
The chances are that you’ve already used a functional. You might have used for-loop replacements like base R’s
tapply(); or purrr’s
map(); or maybe you’ve used a mathematical functional like
A common use of functionals is as an alternative to for loops. For loops have a bad rap in R because many people believe they are slow37, but the real downside of for loops is that they’re very flexible: a loop conveys that you’re iterating, but not what should be done with the results. Just as it’s better to use
repeat, and it’s better to use
while (Section 5.3.2), it’s better to use a functional than
for. Each functional is tailored for a specific task, so when you recognise the functional you immediately know why it’s being used.
If you’re an experienced for loop user, switching to functionals is typically a pattern matching exercise. You look at the for loop and find a functional that matches the basic form. If one doesn’t exist, don’t try and torture an existing functional to fit the form you need. Instead, just leave it as a for loop! (Or once you’ve repeated the same loop two or more times, maybe think about writing your own functional).
Section 9.2 introduces your first functional:
Section 9.3 demonstrates how you can combine multiple simple functionals to solve a more complex problem and discusses how purrr style differs from other approaches.
Section 9.4 teaches you about 18 (!!) important variants of
purrr::map(). Fortunately, their orthogonal design makes them easy to learn, remember, and master.
Section 9.5 introduces a new style of functional:
reduce()systematically reduces a vector to a single result by applying a function that takes two inputs.
Section 9.6 teaches you about predicates: functions that return a single
FALSE, and the family of functionals that use them to solve common problems.
Section 9.7 reviews some functionals in base R that are not members of the map, reduce, or predicate families.
This chapter will focus on functionals provided by the purrr package (Henry and Wickham 2018a). These functions have a consistent interface that makes it easier to understand the key ideas than their base equivalents, which have grown organically over many years. I’ll compare and contrast base R functions as we go, and then wrap up the chapter with a discussion of base functionals that don’t have purrr equivalents.
Henry, Lionel, and Hadley Wickham. 2018a. Purrr: Functional Programming Tools. https://purrr.tidyverse.org.