R, at its heart, is a functional programming (FP) language. This means that it provides many tools for the creation and manipulation of functions.
FP is a technical term that defines certain properties of a programming language. But it’s also a programming style.
In R, the tools for working with functions, the tools of functional programming, are more important than the tools for working with objects or classes, the tools of object oriented programming.
R has what’s known as first class functions. You can do anything with functions that you can do with vectors: you can assign them to variables, store them in lists, pass them as arguments to other functions, create them inside functions, and even return them as the result of a function. A higher-order function is a function that takes a function as an input or returns a function as output. This part of the book is broken down by the three types of higher-order functions:
Functionals, Chapter 10 where functions are the input. Functionals are by far and away the most immediately useful application of FP ideas, and you’ll use them all the time in data analyses.
Function factories, Chapter 11, functions as output. You can almost always avoid function factories in favour of a different technique, but they are occassionally useful.
Function operators, Chapter 12, discusses function as input and output. These are like adverbs, because they typically modify the operation of a function.
18.104.22.168 Other languages
Recently FP has experienced a surge in interest because it provides a complementary set of techniques to object oriented programming, the dominant style for the last several decades. Since FP functions tend to not modify their inputs, they make for programs that are easier to reason about using only local information, and are often easier to parallelise. The traditional weaknesses of FP languages, poorer performance and sometimes unpredictable memory usage, have been largely eliminated in recent years.