Higher Order Functions
· 2 min read
First Class Function
- Every Function in js is a first class function.
Higher Order Function
- Higher-order functions take other functions as input/ and return the output function
- Functions can take functions, and return functions can give functions back.
- Higher order function scan be really useful for another alternative to iteration.
- So you may have already encountered higher-order functions when you've used things like array.map, or array filter, or reduce.
These are methods that we call on JavaScript arrays, butthey are also higher-order functions in the sense that they take in another function as an argument,which they then apply to some kind of iterable object, like an array.
And map, reduce and filter are like three super useful little functions in the functional programmers toolbox because they allow us to really think about ourapplications of what we might usually use an iterativeloop to iterate over an array or something that.
- If I apply a filter to my ingredients array,using my is not gross predicate function, I'll get outa shorter array which only has the things That returned true formy predicate function so bread, not gross, cool.
- Map is also gonna take in a function.it's going to apply that function to each element in the array.I have an operation like cut, or chop orslice, slice like tomatoes.
- Reduce is a little bit different, because unlike filter and map which both take in an array and a function and return another array.Reduced takes in an array in a function, but it returns a single value.