Skip to content

Instantly share code, notes, and snippets.

@BenAychh
Last active October 14, 2016 23:15
Show Gist options
  • Save BenAychh/483440683f04c0adfb0c788240cff320 to your computer and use it in GitHub Desktop.
Save BenAychh/483440683f04c0adfb0c788240cff320 to your computer and use it in GitHub Desktop.
function add(n) {
sum = n;
const proxy = new Proxy(function a () {}, {
get () {
return () => sum;
},
apply (receiver, ...args) {
sum += args[1][0];
return proxy;
}
});
return proxy
}
@BenAychh
Copy link
Author

BenAychh commented Oct 14, 2016

Allows you to add numbers in a functional format, i.e. add(1)(2)(3). Can take unlimited numbers unlike a lot of hard-coded solutions. It also does not need the final empty invocation like many recursive solutions.

console.log(add(1)(2)(3)); // 6
console.log(add(1)(10)(12)(-5)(-13)(100)(1000)(-250)(9)(10)); // 874

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