Last active
October 14, 2016 23:15
-
-
Save BenAychh/483440683f04c0adfb0c788240cff320 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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