- Code Composition and Powerful Examples
- Taking it a Second Step Further
- Taking it a Third Step Further
- Taking it Further One Last Time
- Conclusion
- Source:
A higher order function is a function that either takes another function as an argument or returns a function as the return value.
Higher order functions are widely used in JavaScript and they exist in commonly used functions like .map
, .filter
, .reduce
and .forEach
.
You see these when you declare function callbacks as arguments to these array methods:
1 | const arr = [1, 2, 3, 4, 5, 'six', 'seven', 'eight', 'nine', 'ten'] |
But the higher order function isn’t the function you pass in to methods like .map
. Methods like .map
is the higher order function.
At first, it may seem like a useless way to write code in JavaScript. Why pass in a function and bother returning another function, when you can just avoid all of that and do everything in one function all at once?
The biggest benefit that higher order functions bring to the table are reusability and simplicity. But they also benefit from writing beautiful code.
Code Composition and Powerful Examples
Now that we know what higher order functions look like in code, you might be wondering what were some use cases and where do they begin to shine.
Let’s say we have a list of frogs:
1 | const frogsList = [ |
To filter the frogs to a specific gender type without a higher order function, we would have to do something like this:
1 | function filterGender(gender, frogs) { |
This is perfectly fine. However, it can be cumbersome if used multiple times in an application. If we had a gigantic app about frogs, filterGender
might be used more than once.
Taking it a Second Step Further
If you were to fetch a different list of frogs you’d have to call filterGender
again and re-declare your gender as the first argument to filter the new list:
1 | function getFrogs() { |
To solve this issue, we can use the concept of higher order functions.
1 | function filterGender(gender) { |
And now, just like that, we can just assign this gender filterer to a variable and we would never have to declare the same gender when filtering frogs anymore!
1 | const filterFemaleFrogs = filterGender('Female') |
Taking it a Third Step Further
If you still aren’t convinced enough of how powerful higher order functions are in the JavaScript language, then lets continue the example to make an even more generic function to create a higher level of reusability:
1 | function filterFrogs(filter) { |
Previously we had the ability to make a reusable function for a frog’s gender. However, we can go further by abstracting away the logic of the filter function, so that now we can compose and re-use different filter functions!
1 | const filterMaleFrogs = filterFrogs(function(frog) { |
Wow!
Previously we had the amazing ability to re-use a gender filterer function without ever having to declare the same gender type ever again, but now we have the additional abilities of creating and re-using functions of how we want the frogs to be filtered! Amazing!
We can even use them all at once:
1 | function applyAllFilters(...filters) { |
Taking it Further One Last Time
Our applyAllFilters function does the job quite well. However, for huge lists of frogs it might become a heavy task because it runs filter multiple times to get the final result.
We can again use the concept of higher order functions to make a simple, reusable higher order function that is able to make one pass through the entire list of frogs by applying the filters at the same time.
To be more clear, have a look at the for loop code and try to see what’s truly happening behind the scenes:
1 | function applyAllFilters(...filters) { |
The line I want you to look at is this:
1 | newFrogs = filter(newFrogs) |
That line of code is the same line of code as return frogs.filter(filter) in this function:
1 | function filterFrogs(filter) { |
This is a problem, because the filter method creates a new array. When we had written this:
1 | const applyFrogFilterers = applyAllFilters( |
We’re calling the filter method 4 different times. In other words, we’re making JavaScript create four different arrays in memory just to get the final result.
So how can we make JavaScript create just one array to get the same result in the end?
You guessed it. Using higher order functions!
1 | // NOTE: The filter functions are now individual functions (not wrapped with filterFrogs) |
Conclusion
I hope you are convinced of how powerful higher order functions are and that by reading this article you gained some more insight on the use cases of this concept! Look out more for more in the future!
Source:
https://jsmanifest.com/the-power-of-higher-order-functions-in-javascript/