Created
April 1, 2016 16:39
-
-
Save jslnriot/a978584bfe88b3102cc9121c6c0767a3 to your computer and use it in GitHub Desktop.
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
// 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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Function currying - When you break down a function that takes multiple arguments into a series of functions that take part of the arguments.