-
Notifications
You must be signed in to change notification settings - Fork 3
Higher Order Functions
A fast explanation for a higher-ordered function is: ‘A higher-order function is a function that takes a function as an argument, or returns a function.’ (Elliott, E. | 2019, 10 januari).
Besides the fact that higher-order functions can return a function. In this article I am going to be focussing on a higher-order function that takes a function as an argument. Some functions are sort of ‘built-in’ in Javascript, which are: map, reduce, and filter. I am going to cover this HOF.
The map() method takes an array and puts it through a function (made by the user). Then makes a new array that has been through the given function. For example:
var arr = [1, 4, 9, 16];
// the array
var map = arr.map(function(x){ //calling .map and giving it a function
x = x * 2; // what has to happen in the function
return x
});
console.log(map);
//outcome: Array: [2, 8, 18, 32]You see here how a function takes a function as an argument, a.k.a. higher-order function.
The reduce() method takes each element in an array and runs a function with it (that you give). So that there is one outcome. For example:
const arr = [1, 2, 3, 4];
function reducer(accumulator, currentValue){
return accumulator + currentValue;
};
// here we make the value that the .reduce needs to work
// 1 + 2 + 3 + 4
console.log(arr.reduce(reducer));
// outcome: 10
// 5 + 1 + 2 + 3 + 4
console.log(arr.reduce(reducer, 5));
// outcome: 15You see how reduce takes all the element and trows it in a function (which you provide).
Filter creates a new array with all the elements which went through the ‘test’ (which you provide). For example:
var words = ['hallo', 'yo', 'ey', 'fakka', 'hoi'];
// the array that you are going to use
var result = words.filter(function(word){
return word.length <= 2;
});
// filtering word that are smaller or equal to 2 letters
console.log(result);
// outcome: Array [“yo”, “ey”]You see here that you can filter whatever you want if you write a correct function for it.
- Elliott, E. (2019, 10 januari). Higher Order Functions (Composing Software). Geraadpleegd op 22 mei 2020, van https://medium.com/javascript-scene/higher-order-functions-composing-software-5365cf2cbe99
- Talbot, E. (2019, 4 september). Simplify your JavaScript – Use .map(), .reduce(), and .filter(). Geraadpleegd op 22 mei 2020, van https://medium.com/poka-techblog/simplify-your-javascript-use-map-reduce-and-filter-bd02c593cc2d
- mdnwebdocs-bot. (2019, 7 augustus). Array.prototype.map(). Geraadpleegd op 22 mei 2020, van https://developer.mozilla.org/nl/docs/Web/JavaScript/Reference/Global_Objects/Array/map
- Klement, A. (2013, 14 november). 5 tips for writing a job story. Geraadpleegd op 12 april 2020, van https://jtbd.info/5-tips-for-writing-a-job-story-7c9092911fc9
- Anonymous. (2020, 20 mei). Array.prototype.filter(). Geraadpleegd op 22 mei 2020, van https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter