Skip to content

Instantly share code, notes, and snippets.

@meandmax
Last active April 19, 2018 09:33
Show Gist options
  • Save meandmax/a54194843f575618d48bb6f00b408672 to your computer and use it in GitHub Desktop.
Save meandmax/a54194843f575618d48bb6f00b408672 to your computer and use it in GitHub Desktop.
Add Rekursion
const add = (y) => {
return (x) => {
if (!x) {
return y;
}
return add(y + x);
}
}
// add(1)(2)(3)()
function add(y) {
let sum = 0;
function add (x) {
for (let i=0; i<arguments.length; i++) {
sum += Number(arguments[i]);
}
return add;
}
add.valueOf = function valueOf(){
return sum;
};
return add.apply(null,arguments);
}
// add(1)()(3)
// even easier ...
const add = x => {
const inner = y => add(x + y);
inner.valueOf = () => x;
return inner;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment