Last active
April 29, 2016 18:43
-
-
Save jjsub/f79a20d44f3c9d9c85dcd7024cfb68df to your computer and use it in GitHub Desktop.
Learning javascript curry
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
// Learning javascript curry | |
// see http://www.sitepoint.com/currying-in-functional-javascript/ | |
/*Briefly, currying is a way of constructing functions that allows partial application of a function’s arguments. | |
What this means is that you can pass all of the arguments a function is expecting and get the result, | |
or pass a subset of those arguments and get a function back that’s waiting for the rest of the arguments. | |
It really is that simple. */ | |
// Example | |
var saludosCurry = function(saludo) { | |
return function(nombre) { | |
console.info(saludo + " , " + nombre); | |
}; | |
}; | |
var saludo = saludosCurry("Hola"); | |
saludo("Pepe"); // Hola, Pepe | |
/* We can also call the original curried function directly, just by passing each of the parameters in a separate set of | |
parentheses, one right after the other: */ | |
saludosCurry("Hola")("Juan"); // Hola, Juan | |
//cool |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
We can also call the original curried function directly, just by passing each of the parameters in a separate set of parentheses, one right after the other:
saludosCurry("Hola")("Juan"); // Hola, Juan