function partial(fn) {
var args = [];
for (var i = 1; i < arguments.length; i++) {
args.push(arguments[i]);
}
i = 1;
return function() {
for (var j = 0; j < arguments.length; j++) {
args.push(arguments[j]);
}
j = 0;
return fn.apply(null, args);;
}
}
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
ZnVuY3Rpb24gcGFydGlhbChmbikgewoJCXZhciBhcmdzID0gW107CgkJZm9yICh2YXIgaSA9IDE7IGkgPCBhcmd1bWVudHMubGVuZ3RoOyBpKyspIHsKIAkJYXJncy5wdXNoKGFyZ3VtZW50c1tpXSk7CiAJCX0KIAkJaSA9IDE7CglyZXR1cm4gZnVuY3Rpb24oKSB7CgkJZm9yICh2YXIgaiA9IDA7IGogPCBhcmd1bWVudHMubGVuZ3RoOyBqKyspIHsKIAkJYXJncy5wdXNoKGFyZ3VtZW50c1tqXSk7CiAJCX0KIAkJaiA9IDA7CgkJcmV0dXJuIGZuLmFwcGx5KG51bGwsIGFyZ3MpOzsKCX0KfQoKCgoKZnVuY3Rpb24gYWRkKGEsIGIpIHsgcmV0dXJuIGEgKyBiOyB9CmZ1bmN0aW9uIG11bHQoYSwgYiwgYywgZCkgeyByZXR1cm4gYSAqIGIgKiBjICogZDsgfQoKdmFyIGFkZDUgPSBwYXJ0aWFsKGFkZCwgNSk7IC8vINCc0Ysg0L/QvtC70YPRh9C40LvQuCDRhNGD0L3QutGG0LjRjiDRgSAxINCw0YDQs9GD0LzQtdC90YLQvtC8LCDQutC+0YLQvtGA0LDRjyDQv9GA0LjQsdCw0LLQu9GP0LXRgiDQuiDQu9GO0LHQvtC80YMg0YfQuNGB0L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