Skip to content

Instantly share code, notes, and snippets.

@ayinlaaji
Last active April 26, 2018 13:39
Show Gist options
  • Save ayinlaaji/032ff79cbc438cf450d2d1ab42546434 to your computer and use it in GitHub Desktop.
Save ayinlaaji/032ff79cbc438cf450d2d1ab42546434 to your computer and use it in GitHub Desktop.
Playing with currying
// 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