Last active
September 2, 2020 04:11
-
-
Save getify/fa0c953ceae448177caf to your computer and use it in GitHub Desktop.
an `add(..)` function that keeps going forever :)
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() { | |
var sum = 0; | |
function add() { | |
for (var i=0; i<arguments.length; i++) { | |
sum += Number(arguments[i]); | |
} | |
return add; | |
} | |
add.valueOf = function valueOf(){ | |
return sum; | |
}; | |
return add.apply(null,arguments); | |
} | |
// ... | |
add() + 0; // 0 | |
add(1) + 0; // 1 | |
add(1,2) + 0; // 3 | |
add(1)(2) + 0; // 3 | |
add(1,2)(3) + 0; // 6 | |
add()()(1,2)()(3) + 0; // 6 | |
add(1,2)(3)(4,5)(6) + 0; // 21 |
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
// 96 characters, from @sainaen, @rreverser, others: | |
function add(a,s){return(a=add.bind(0,s=[].join.call(arguments,'+'))).toString=eval.bind(0,s),a} | |
add() + 0; // NaN | |
add(1) + 0; // 1 | |
add(1,2) + 0; // 3 | |
add(1)(2) + 0; // 3 | |
add(1,2)(3) + 0; // 6 | |
add()()(1,2)()(3) + 0; // 6 | |
add(1,2)(3)(4,5)(6) + 0; // 21 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment