1. Functional programming in R
Before diving into the functional programming tools provided by purrr, let's start with a brief introduction to what functional programming is.
2. About computation in R
Let's start this lesson with a quote from John Chambers, the creator of the S language, and one of the most important members of the R project: "To understand computations in R, two slogans are helpful: Everything that exists is an object. Everything that happens is a function call."
What this quote means is that every element that exists in R is an object, in the sense of a data structure, composed of a name and a value. This also means that a function is an object, and it can be treated and manipulated as such.
The second part of this quote means that every computation that happens in R is due to a function. In other words, every single action that is performed in R is performed by a call to a function.
Functions are first-class citizens in R, which means that functions behave just like any other data structure. So yes, using the plus operator is a function call, as is the assignment operator.
3. R as a functional programming language
Functions are the central data structure when it comes to programming — when programming, we are designing tools that perform actions on data.
In R, as we've just said, functions are objects. This specificity means that they can be manipulated, like any other object, with specific tools — this is what we are going to see in the rest of this course: how to manipulate functions easily with purrr, and how to use these newly created tools.
As functions are objects, they behave as such: they can be assigned to a variable, or as we‘ve seen in the previous chapter, they can be called just once, being then a lambda / anonymous function.
Like any other object, they can be stored in a list, and they can be used as an argument to another function. Note that they can also be returned by another function. In other words, you can create and use functions that return another function, as we will see in this chapter.
4. About "pure functions"
Have you ever wondered why purrr is called purrr? The first baseline of the package was: "Make your pure functions purr with purrr, a functional programming library for R."
So yes, purrr was first designed to handle pure functions. The concept of purity of functions is not tied to R but exists in almost every programming language. A function is said to be pure when it has two main properties. First, the output only depends on the inputs, and the inputs only. For example, the sum() or the mean() functions only depend on their inputs.
In other words, everything needed inside a function should be passed as a parameter. Secondly, a pure function should not have any side effects, that is to say, no change in the environment.
5. Impure functions are useful
Note that a lot of functions do not satisfy these input/output and "side effect" conditions. Think about download.file(), plot(), read.csv(), and others, which are not pure but necessary for data analysis: it's obvious you'll need to read or write files, or to create plots.
6. Read more about functional programming
If you want to know more about functional programming in R, here are resources you can dive into for more information.
7. Let's practice!
Let's start this chapter with a little Q&A about functional programming in R.