Created
December 31, 2021 20:19
-
-
Save luizcalaca/f6424e5b4f8a568fb767e9705d63d86f to your computer and use it in GitHub Desktop.
Javascript Currying
This file contains 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
//returning many functions result | |
function sum(a){ | |
return function(b){ | |
return function(c){ | |
return a + b + c | |
} | |
} | |
} | |
let result = sum(2)(5)(1) | |
//let's refactor the code above | |
const result = (a) => { | |
return (b) => { | |
return (c) => { | |
return a + b + c | |
} | |
} | |
} | |
console.log(result(3)(3)(3)) | |
//let's refactor one more time | |
const sum = a => b => c => a + b + c | |
//all returns the same result | |
console.log(sum(1)(0)(0)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A biblioteca Ramda possui uma função, R.curry, que recebe uma função e retorna uma versão curried dessa função.