Created
February 1, 2021 10:43
-
-
Save elstr/ddb79e69b430bdfceafc0a293836def4 to your computer and use it in GitHub Desktop.
Higher Order Function Examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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