Skip to content

Instantly share code, notes, and snippets.

@tebba-von-mathenstein
Created June 28, 2017 17:57
Show Gist options
  • Save tebba-von-mathenstein/af31fb57b072438f213687e87e6618d4 to your computer and use it in GitHub Desktop.
Save tebba-von-mathenstein/af31fb57b072438f213687e87e6618d4 to your computer and use it in GitHub Desktop.
Just one odditiy of JS
function CallableNumberGenerator(value, func) {
var selfFunction = function() {
var v = func.apply(null, arguments);
return CallableNumberGenerator(v, func);
}
var callableNumber = selfFunction.bind(null, value);
var value = value;
callableNumber.valueOf = function(){ return value };
return callableNumber;
}
function add() {
var value = 0;
for(idx in arguments) {
value += arguments[idx];
}
return value;
}
function multiply(){
var value = 1;
for(idx in arguments) {
value *= arguments[idx];
}
return value;
}
var addSeries = CallableNumberGenerator(1, add);
var multSeries = CallableNumberGenerator(2, multiply);
// Initial value remains the same for a given series
console.log(addSeries); // 1
console.log(addSeries(1)); // 2
console.log(addSeries(5,6,7)); // 19
var cpy = addSeries(5, 6, 7)(10)(4)(3, 2)(9);
console.log(cpy + 2);
cpy(3);
console.log(cpy == 47);
console.log(multSeries(2));
console.log(multSeries(2)(2, 4));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment