var add = function (a, b) { return a + b; };
var mul = function (a, b) { return a * b; };
var make = (function() {
var cache = [],
local = [],
cacheHandler;
cacheHandler = function(value) {
if (typeof value === 'function') {
local = [];
while(cache.length != local.length) {
local.push(cache[local.length]);
}
do {
local.unshift(
value(local.shift(), local.shift())
);
} while (local.length != 1);
return local.shift();
} else {
cache.push(value);
return cacheHandler;
}
}
return cacheHandler;
}());
var s = make(1)(2)(3)(4)(5);
console.log(s(add) === 15);
console.log(s(mul) === 120)
а как можно еще круче?
например:
10x