function partial(fn) {
var args = [];
for (var i = 1; i < arguments.length; i++) {
args.push(arguments[i]);
}
return function() {
for (var j = 0; j <= arguments.length; j++) {
args.push(arguments[j]);
}
x = fn.apply(null, args);
args.splice(i - 1,args.length);
return x;
}
}
function add(a, b) { return a + b; }
function mult(a, b, c, d) { return a * b * c * d; }
var add5 = partial(add, 5); // Мы получили функцию с 1 аргументом, которая прибавляет к любому числу 5
console.log(add5(2)); // 7
console.log(add5(10)); // 15
console.log(add5(8)); // 13
var mult23 = partial(mult, 2, 3); // мы зафиксировали первые 2 аргумента mult() как 2 и 3
console.log(mult23(4, 5)); // 2*3*4*5 = 120
console.log(mult23(1, 1)); // 2*3*1*1 = 6
ZnVuY3Rpb24gcGFydGlhbChmbikgewkKCXZhciBhcmdzID0gW107CgoJZm9yICh2YXIgaSA9IDE7IGkgPCBhcmd1bWVudHMubGVuZ3RoOyBpKyspIHsKIAkJYXJncy5wdXNoKGFyZ3VtZW50c1tpXSk7CiAJfQoKCXJldHVybiBmdW5jdGlvbigpIHsKCQlmb3IgKHZhciBqID0gMDsgaiA8PSBhcmd1bWVudHMubGVuZ3RoOyBqKyspIHsKIAkJYXJncy5wdXNoKGFyZ3VtZW50c1tqXSk7CiAJCX0KIAkJCiAJCXggPSBmbi5hcHBseShudWxsLCBhcmdzKTsKIAkJYXJncy5zcGxpY2UoaSAtIDEsYXJncy5sZW5ndGgpOwoJCXJldHVybiB4OwoJfQp9CgoKCgpmdW5jdGlvbiBhZGQoYSwgYikgeyByZXR1cm4gYSArIGI7IH0KZnVuY3Rpb24gbXVsdChhLCBiLCBjLCBkKSB7IHJldHVybiBhICogYiAqIGMgKiBkOyB9Cgp2YXIgYWRkNSA9IHBhcnRpYWwoYWRkLCA1KTsgLy8g0JzRiyDQv9C+0LvRg9GH0LjQu9C4INGE0YPQvdC60YbQuNGOINGBIDEg0LDRgNCz0YPQvNC10L3RgtC+0LwsINC60L7RgtC+0YDQsNGPINC/0YDQuNCx0LDQstC70Y/QtdGCIN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