Skip to content

Instantly share code, notes, and snippets.

@ross-u
Created May 24, 2019 09:21
Show Gist options
  • Save ross-u/16791e5c4738870666a696334f600db5 to your computer and use it in GitHub Desktop.
Save ross-u/16791e5c4738870666a696334f600db5 to your computer and use it in GitHub Desktop.
Higher order function examples
function outter () {
return function(wordToPrint) {
console.log('outter()(wordToPrint) -->', wordToPrint)
}
}
// Invoke `outter` and save returned value (function) in a variable
const inner = outter();
inner('Hello');
// Or we can do everything in one expression
outter()('Bye');
// Same pattern with additional function returning a function
function triple () {
return function() {
return function(wordToPrint) {
console.log('triple()()(wordToPrint) -->', wordToPrint)
}
}
}
triple()()('hi hi hi')
// Same pattern where we pass parameter to each function
function doubleWithParams (firstWord) {
return function (secondWord) {
console.log('doubleWithParams(firstWord)(secondWord) -->', firstWord + secondWord)
}
}
doubleWithParams('Bat')('man');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment