Created
November 8, 2012 05:55
-
-
Save shuhrat/4037080 to your computer and use it in GitHub Desktop.
This file contains 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
var s = make(1)(2)(3)(4)(5); | |
var add = function (a, b) { return a + b; }; | |
var mul = function (a, b) { return a * b; }; | |
s(add); //15 | |
s(mul); // 120 | |
//Write make =) |
так лучшее будет
function make(a) {
var num = [];
if (!isNaN(a)) {
num.push(a);
function f(b) {
if (!isNaN(b)) {
num.push(b);
return f;
} else {
var result = num[0];
for (var i = 1; i < num.length; i++){
result = b(result, num[i]);
}
return result;
}
}
return f;
} else
return null;
}
var make = (function() {
var cache = [],
cacheHandler,
result, max;
cacheHandler = function(value) {
if (typeof value === 'function') {
result = null;
max = cache.length;
if (max >= 2) {
result = cache[--max];
while(max--) {
result = value(result, cache[max]);
}
}
return result;
} else {
cache.push(value);
return cacheHandler;
}
}
return cacheHandler;
}());
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Особо не заморачивался, первое решение выглядит как-то так:
Отличная задача для собеседований, кстати. Можно сказать идеальная.