Skip to content

Instantly share code, notes, and snippets.

@johnkpaul
Created August 29, 2015 13:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnkpaul/82b5513768a65f6c4dcc to your computer and use it in GitHub Desktop.
Save johnkpaul/82b5513768a65f6c4dcc to your computer and use it in GitHub Desktop.
function add() {
var args = [].slice.call(arguments);
var sum = args.reduce(function(a, b){ return a + b; }, 0);
return numify(sum);
function numify(num){
var ret = function(a){
return a === undefined ? numify(num) : numify(a + num);
}
ret.valueOf = function(){ return num; };
return ret;
}
}
test(add(1, 2), 3);
test(add(3)(4)(), 7);
test(add(3)(4)(5), 12);
var three = add(3);
var four = add(4);
test(three, 3);
test(four, 4);
test(three(5), 8);
test(three(6), 9);
test(three(four), 7);
test(three(four)(three(four)), 14);
function test(a, b) {
if (a == b) {
console.log('OK');
} else {
console.log(a + ' != ' + b);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment