Skip to content

Instantly share code, notes, and snippets.

@jslnriot
Created April 1, 2016 16:39
Show Gist options
  • Save jslnriot/a978584bfe88b3102cc9121c6c0767a3 to your computer and use it in GitHub Desktop.
Save jslnriot/a978584bfe88b3102cc9121c6c0767a3 to your computer and use it in GitHub Desktop.
// Use this to create a generic function that you can specialize later
// This example takes only 1 argument
function add(firstNumber) {
//var addMe = firstNumber;
return function(secondNumber) {
return firstNumber + secondNumber;
}
}
var twenty = add(12)(8);
console.log(twenty);
var addFive = add(5);
console.log(addFive);
var fifteen = addFive(10);
console.log(fifteen);
var addTwelve = add(12);
console.log(addTwelve(30)); // 42
console.log(addTwelve(100)); //112
var greet = add("Hi");
console.log(greet("Chris"));
console.log("---------------------");
// This example can be called different ways
function addFlexible(num1, num2) {
if(num2){
return num1 + num2;
}
var addMe = num1;
return function(num1) {
return addMe + num1;
}
}
var twentyOne = addFlexible(13,8);
console.log("Should be 21: " +twentyOne);
var twentyTwo = addFlexible(13)(9);
console.log("Should be 22: " + twentyTwo);
@jslnriot
Copy link
Author

jslnriot commented Apr 1, 2016

Function currying - When you break down a function that takes multiple arguments into a series of functions that take part of the arguments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment