Skip to content

Instantly share code, notes, and snippets.

@mateuszsokola
Created July 28, 2017 17:56
Show Gist options
  • Save mateuszsokola/3079d2d98545c200496207a602236abd to your computer and use it in GitHub Desktop.
Save mateuszsokola/3079d2d98545c200496207a602236abd to your computer and use it in GitHub Desktop.
Higher Order Functions

Higher Order Functions

Higher Order Functions (HOF) take one or more functions as arguments and they must be trigger in the first order. They often return function as the output.

When I use HOF?

  • Defining callbacks
  • Composition
  • map / reduce

Before going forward please read about Composition.

const array = [1,2,3,4,5,6,7,8,9];
// filter is higher order function as it takes a function as a param
const evenNums = array.filter(num => num % 2 === 0);
console.log('even numbers in array: ', ...evenNums);
// higher order functions often return other functions.
function calculate (f) {
return function (a, b) {
return f(a, b);
}
}
// function composition :)
const sum = calculate((a, b) => a + b);
console.log('sum: ', sum(1, 2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment