Skip to content

Instantly share code, notes, and snippets.

@srikumarks
Last active April 11, 2017 14:30
Show Gist options
  • Select an option

  • Save srikumarks/3ef88fa755b81f2cecf5 to your computer and use it in GitHub Desktop.

Select an option

Save srikumarks/3ef88fa755b81f2cecf5 to your computer and use it in GitHub Desktop.
Currying sum hack
// 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