What You Should Know About Functional Programming
A common interview question is being able to understand functional programming. How is it used? What are the main points you need to keep in mind? I discuss all that and more below.
What are Pure Functions?
Functional programming (FP) is a way to organize code using pure functions. A function is pure if, given the same input, it returns the same output. Evaluating the function should also have zero “side effects” (eg., changing variables outside the function scope OR updating other parts of the program, such as storage). For example, the function below is pure because it will always return the same output if given the same input.
const triple = num = {
return num * 3
}
The function below is not pure because the variable num is mutated, which causes a “side effect” in executing the code.
let num = 46const toString = int = {
num = int
return int.toString()
}
Immutable Data in Functional Programming
Another important concept to understand in FP is immutable data. In FP, we want to try to avoid mutating data as much as possible. One such way to work with data without mutating it in FP is through higher order functions. We can use higher order functions, such as .map
, which takes in the function addLetter
as an argument below:
const data = [1,2,3,4,5,]const addLetter = int => toString(int) + 'A'const firstFlrApts = data.map(addLetter)// ['1A', '2A', '3A', '4A', '5A']
Since .map
returns a new array, we do not mutate the original array. Other popular higher order functions in functional programming are .filter
and .reduce
.
Declarative vs. Imperative
Functional programming is declarative. Declarative programming describes what to do. Imperative programming describes how to do it. Let’s consider a metaphor:
Friend: “I see you’re almost done with dinner. Anything I can do to help?”
Declarative response (WHAT): “Set the table.”
Imperative response (HOW): “You can grab a placemat, napkin, fork, knife, cup, and plate for each person at the table. Position the placemats on the table directly across from each chair. Fold each napkin in half and place on the right side of the placemat. Lay a fork and knife on top of the folded napkin…”
Now let’s take a look at how this applies to code. Consider the following question, and then take a look at imperative and declarative solutions.
Write a function called
triple
that takes in an array of numbers and returns a new array after tripling every element in the array.
Imperative solution
const triple = numArr => {
let tripledArr = []
for (let i = 0; i < numArr.length; i++){
tripledArr.push(numArr[i] * 3)
}
return tripledArr
}
Declarative solution
const triple = numArr => {
return numArr.map(num => num * 3)
}
Notice that the declarative solution is far more readable and it does NOT mutate state.
On readability: Sure, the code is shorter, and that’s great, but a challenge with declarative programming is that the process is abstracted away. So, you have to make sure you understand what’s happening under the hood.
On immutability: As discussed earlier, functional programming favors immutable data. The solution above uses .map to create a brand new array, so we avoid mutating state.
This post covered some of the top points to know about functional programming, but there’s still plenty more to learn. For more information, check out my sources below.
Sources: