3
I understand that, roughly speaking, to use currying is to break a function that takes several arguments into smaller functions that receive only parts of the arguments of the original function. Consider the code below as an example:
window.onload = function () {
//função de somar com a sintaxe comum
console.log("Função comum: " + soma(1,1));
//função de somar com a sintaxe "currificada"
console.log("Função com curry: " + somacurry(2)(2));
//função que exibe as informações da pessoa também com curry
console.log(pessoa("Artur")("Trapp")("21"));
}
function soma(a,b) { return a + b; }
function somacurry(a){
return function (b) { return a + b; }
}
function pessoa(nome){
return function (sobrenome){
return function (idade) {
return "Olá, meu nome é " + nome + " " + sobrenome + ", e eu tenho " + idade + " anos!";
}
}
}
I know that this technique is widely used in functional languages (such as Haskell). But I’m not able to see the advantages, whether in simplifying syntax or in performance. What would be the advantages of using a function with the currying technique, rather than functions with common syntax?
PS: I did not find ideal tags