Skip to content

Instantly share code, notes, and snippets.

@elstr
Created February 1, 2021 10:43
Show Gist options
  • Select an option

  • Save elstr/ddb79e69b430bdfceafc0a293836def4 to your computer and use it in GitHub Desktop.

Select an option

Save elstr/ddb79e69b430bdfceafc0a293836def4 to your computer and use it in GitHub Desktop.
Higher Order Function Examples
// higher order function
// es una funcion que acepta una funcion como parametro
// puede o no retornar otra funcion
// pure => dado un valor x siempre me devuelve un valor y
function multiplicarPor(factor){
return function(valor) {
return valor * factor
}
}
var duplicar = multiplicarPor(2) // => multiplicarPor(factor)
/* console.log(duplicar(4)) */
/*
var quintuplicar = multiplicarPor(5)
console.log(quintuplicar(5)) */
// HOC => higher order component REACT
// map / filter / find / reduce
// ejemplo de higher order function en al vida real => MAP
/* var arrDuplicado = [ 1, 2, 3 ].map(duplicar); */
/* console.log(arrDuplicado); // [ 2, 4, 6 ] */
// redux => decorator => connect ()
const withCount = fn => {
let count = 0
return (...args) => {
console.log(`Call count ${++count}`)
return fn(...args)
}
}
const add =(x,y)=> x+y
const countAdd = withCount(add)
console.log(countAdd(2,3))
console.log(countAdd(4,4))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment