Higher Order Functions
In JavaScript, a higher order function is a function that takes a function as an argument or returns a function as a result. In other words, a higher order function is a function that operates on other functions.
To understand Higher-Order Functions, let's start with an example. Let's say you want to write a function that takes an array of numbers and returns a new array with all the numbers doubled. You could write a function like this:
This function takes an array of numbers as an argument and returns a new array with all the numbers doubled. It is a pure function because it doesn't modify any data outside of its scope, and its output depends only on its input.
This function works, but what if you want to triple the numbers instead of doubling them? Or what if you want to square the numbers? You would have to write a new function for each case.
This is where Higher-Order Functions come in! In this case, instead of writing a new function for each case, you can write a function that takes another function as an argument and uses it to transform the numbers. Here's an example:
Now, we have a function called transformNumbers
that takes an array of numbers and a function as arguments. The function then iterates over the array and applies the function to each number. It returns a new array with the transformed numbers.
We also have three functions called double
, triple
, and square
that take a number as an argument and return the number doubled, tripled, and squared, respectively.
Now, we can use the transformNumbers
function to transform the numbers in different ways:
In the above example, we use the transformNumbers
function to transform the numbers in three different ways: doubling, tripling, and squaring. We pass the double
, triple
, and square
functions as arguments to the transformNumbers
function.
So, in this example, the transformNumbers
function is a higher order function because it takes a function as an argument.
Why use higher order functions?
Higher order functions are useful because they allow us to write more generic functions that can be used to solve a variety of problems. For example, the transformNumbers
function can be used to transform the numbers in any way we want, as long as we pass a function that transforms the numbers as an argument.
In contrast, if we had written a function for each case, we would have to write a new function for each new case, repeating a lot of code (like the loop and the push
method). This would make our code less maintainable.
Code Challenge
Write a function applyAndIncrement
that takes two parameters:
value
(a number)func
(a higher-order function that takes a number and returns a number)
The applyAndIncrement
function should apply the func
function to value
and then return the result incremented by 1.
For example:
In the first example, applyAndIncrement(3, addTwo)
should return 6
, which is the result of addTwo(3)
(which is 5
) incremented by 1
.
In the second example, applyAndIncrement(3, multiplyByFive)
should return 16
, which is the result of multiplyByFive(3)
(which is 15
) incremented by 1
.