library(purrr)
It actually took me a while to understand what functional programming is. So to give it a good start without getting too technical, I will start with a simple example.
Functional programming is like using a microwave. You put something in, press a button, and get something out. You don’t need to know how it works—the microwave takes care of that for you. You just need to know what buttons to press. This is the essence of functional programming. Whatever matters in this context is the ingredients of the food (data and input for the function) that you put together and place it inside the microwave, the function of the microwave that you choose (function(s)). The output is the dish that you get (output of the function). In an essence, you need input, function, and output.
What is a function?
A function is a block of code that performs a specific task. It takes in some input, performs some processing, and produces some output. A function is a self-contained block of code that encapsulates a specific task or related group of tasks. Functions usually “do” something, and they usually “return” something. The “something” that a function returns is called its return value. A function can have
- zero or more parameters. Parameters are inputs to the function;
- zero or more return values. Return values are outputs from the function.
- zero or more side effects. Side effects are outputs from the function that are not return values.
R as a functional programming language, does not require you to write pure functions. A pure function is a function that has no side effects and always returns the same output for the same input. Imagine if you would like to generate random number from the function, then it is not a pure function. However, it is still a function.
A functional is a function that takes a function as an input and returns a vector as output.
The function that is passed in as an input is called a function argument. The function that is returned as an output is called a function output. There are lots of examples of functionals can be found in purrr
package. For example, map()
is a functional that takes a function as an input and returns a vector as output.
For simple illustration, let’s focus on the map()
function.
<- function(x) x^2
square map(1:3, square)
[[1]]
[1] 1
[[2]]
[1] 4
[[3]]
[1] 9
Explanation
- First, a
square
function is created.
- Second, the
map()
function is called.
- Then, the first argument is a vector of 1 to 3. The second argument is the
square
function. - The
map()
function takes thesquare
function as an input and returns a vector as output. - The
map()
function applies thesquare
function to each element of the vector of 1 to 3. - So, it takes 1 and returns 1^2, then it takes 2 and returns 2^2, and so on.
- Finally, the
map()
function returns a vector of 1, 4, and 9 after it run thesquare
function on each element of the vector of 1 to 3.
So let’s stop here and have a grasp on the concept of functional programming, before we explore more of this type of functions. To be continued…
To read more about functional programming, please visit Hadley Wickham’s Advanced R book, chapter 9 to 11.
Citation
@online{tan2024,
author = {Tan, Joan},
title = {What Is Functional Programming {(Part} {I)}},
date = {2024-01-14},
url = {https://joantan.org/R/2024-01-14-functional-programming/},
langid = {en}
}