Last active
April 11, 2017 14:30
-
-
Save srikumarks/3ef88fa755b81f2cecf5 to your computer and use it in GitHub Desktop.
Currying sum hack
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
| // The problem is to write sum() such that it produces the following output - | |
| // console.log(sum(2,3)); // Produces 5 | |
| // console.log(sum(2)(3)); // Produces 5 | |
| function sum() { | |
| var s = Array.prototype.reduce.call(arguments, function (x, y) { return x + y; }, 0); | |
| var f = function () { | |
| var a = Array.prototype.slice.call(arguments); | |
| a.push(s); | |
| return sum.apply(null, a); | |
| }; | |
| f.valueOf = function () { return s; }; | |
| return f; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment