ABSTRACT

In chapter 9 we’ll learn about functionals. A functional is a function that takes a function as an input and returns a vector as output. A common use of functionals is as an alternative to for loops. As Loops do not convey what should be done with the results, it’s better to use a functional. Each functional is tailored for a specific task, so when you recognise the functional you immediately know why it’s being used.

“My first functional: map()” introduces your first functional: purrr::map(). “Purrr style” demonstrates how you can combine multiple simple functionals to solve a more complex problem and discusses how purrr style differs from other approaches. “Map variants” teaches you about 18 (!!) important variants of purrr::map(). Fortunately, their orthogonal design makes them easy to learn, remember, and master. “Reduce family” introduces a new style of functional: purrr::reduce(). reduce() systematically reduces a vector to a single result by applying a function that takes two inputs. “Predicate functionals” teaches you about predicates: functions that return a single TRUE or FALSE, and the family of functionals that use them to solve common problems. “Base functionals” reviews some functionals in base R that are not members of the map, reduce, or predicate families.