add
"", , , . , add
, , , .
:
add(1,2,3)
, , add function
:
function curry(fn) {
var args = Array.prototype.slice.call(arguments, 1);
return function () {
return fn.apply(this, args.concat(
Array.prototype.slice.call(arguments, 0)
));
}
}
function add() {
var args = Array.prototype.slice.call(arguments);
return args.reduce(function (previousValue, currentValue) {
return previousValue + currentValue;
});
}
, , :
var curry1 = curry(add, 1);
console.log(
curry1(2),
curry1(2, 3),
curry1(4, 5, 6)
);
var curry15 = curry(add, 1,2,3,4,5);
console.log(curry15(6,7,8,9));
1, 2, 3
, :
add(1,2,3)
.
curry, , - .
, , , :
function multiply() {
var args = Array.prototype.slice.call(arguments);
return args.reduce(function (previousValue, currentValue) {
return previousValue * currentValue;
});
}
multiply(2,4,8)
var curryMultiply2 = curry(multiply, 2);
curryMultiply2(4,8)
, . curry
, , , .