Last active
April 26, 2018 13:39
-
-
Save ayinlaaji/032ff79cbc438cf450d2d1ab42546434 to your computer and use it in GitHub Desktop.
Playing with 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
// Understanding currying | |
// Github Git @ayinlaaji | |
const curry = funcToBeCurried => { | |
const noArgs = funcToBeCurried.length; | |
return doYouHaveMore([]); | |
//Hoisting :D | |
function doYouHaveMore(prevArgs) { | |
return function askForMore() { | |
const moreArgs = [].slice.call(arguments, 0); | |
const totalArgs = prevArgs.concat(moreArgs); | |
return totalArgs.length < noArgs | |
? doYouHaveMore(totalArgs) | |
: funcToBeCurried.apply(null, totalArgs); | |
}; | |
} | |
}; | |
const theBeast = (a, b, c) => a + b + c; | |
const curriedBeast = curry(theBeast); | |
const res0 = curriedBeast(1, 2, 3); | |
const res1 = curriedBeast(1)(2)(3); | |
const res2 = curriedBeast(1)(2, 3); | |
//var res3 = curriedBeast(1, 2)(3); //Last variation | |
const markOfTheBeast = `${res0}${res1}${res2}`; | |
console.log(`${markOfTheBeast} :D`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment