Table of contents
No headings in the article.
What is Higher Order Function (HOF) ?
Before going to understand higher order functions, let's see why we need HOF and what kind of problem it's solves.
let's take simple example, consider one use case where you need to double every element of an array.
suppose now similarly you want to do addition, subtraction and division on each element.
This approach will works fine, but there is a problem with this approach that is we are repeating same things many times (creating output var, iterating over numbers array and returning the final output). Our business logic is different and other things are same only in all cases.
so basically here we are breaking DRY Principles (Do Not Repeat Yourself).
How we can fix this ? so here we can use HOF functions to solve this issue.
Higher Order Functions are the Functions that takes Function as an argument and/or returns other Function from it
This concepts is used a lot in Function Programming.
let's take an example.
now As we can see that we have abstracted out business logic and we are passing these Functions as an argument to the higherOrderFunction, so now we are not breaking DRY principles.
what are the benefits of HOF ?
improves the code readability
makes code easy to debug
avoids same code repetition
It's makes our code more reusable
we are using Higher Order Functions in our day to day life. Higher Order Functions are so much important. The most useful functions like map, filter, reduce etc.. on array are nothing but Higher Order Functions.
Thank you so much for reading this Blog.