function partial(fn) {
var args = Array.prototype.slice.call(arguments);
var args = args.slice(1);
return function() {
var args1 = Array.prototype.slice.call(arguments);
var newArgs = args.concat(args1);
var x = fn.apply(null, newArgs);
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
ZnVuY3Rpb24gcGFydGlhbChmbikgewkKCXZhciBhcmdzID0gQXJyYXkucHJvdG90eXBlLnNsaWNlLmNhbGwoYXJndW1lbnRzKTsKCXZhciBhcmdzID0gYXJncy5zbGljZSgxKTsKCglyZXR1cm4gZnVuY3Rpb24oKSB7CgkJdmFyIGFyZ3MxID0gQXJyYXkucHJvdG90eXBlLnNsaWNlLmNhbGwoYXJndW1lbnRzKTsKIAkJdmFyIG5ld0FyZ3MgPSBhcmdzLmNvbmNhdChhcmdzMSk7CiAJCXZhciB4ID0gZm4uYXBwbHkobnVsbCwgbmV3QXJncyk7CgkJcmV0dXJuIHg7Cgl9Cn0KCgpmdW5jdGlvbiBhZGQoYSwgYikgeyByZXR1cm4gYSArIGI7IH0KZnVuY3Rpb24gbXVsdChhLCBiLCBjLCBkKSB7IHJldHVybiBhICogYiAqIGMgKiBkOyB9Cgp2YXIgYWRkNSA9IHBhcnRpYWwoYWRkLCA1KTsgLy8g0JzRiyDQv9C+0LvRg9GH0LjQu9C4INGE0YPQvdC60YbQuNGOINGBIDEg0LDRgNCz0YPQvNC10L3RgtC+0LwsINC60L7RgtC+0YDQsNGPINC/0YDQuNCx0LDQstC70Y/QtdGCINC6INC70Y7QsdC+0LzRgyDRh9C40YHQu9GDIDUKCmNvbnNvbGUubG9nKGFkZDUoMikpOyAv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