// Currying是函数式编程的一个特性,将多个参数的处理转化成单个参数的处理,类似链式调用。
// 柯里化(Currying)情景一:
// 第一个参数为要应用的function,第二个参数是需要传入的最少参数个数
function curry(func, outerArgs) {
if (outerArgs == undefined) {
outerArgs = 1;
}
function funcTempInvoke(innerArgs) {
return function () {
// 优化处理,如果调用时没有参数,返回该函数本身
var args = Array.prototype.slice.call(arguments);
var newArgs = innerArgs.concat(args);
if (newArgs.length >= outerArgs) {
return func.apply(this, newArgs);
} else {
return funcTempInvoke(newArgs);
}
};
}
return funcTempInvoke([]);
}
function testCurry01(){
//这里定义了一个插件式函数;
var plus = curry(function () {
var result = 0;
for (var i = 0; i < arguments.length; ++i) {
result += arguments[i];
}
return result;
}, 2);
console.log("-----------------------------start01-------------------------------");
var a1 = plus(3, 2); // 正常调用
console.log(a1);
var a2 = plus(3); // 偏应用,返回一个函数(返回值为3+参数值)
console.log(a2);
var a3 = plus(3)(2); // 完整应用(返回5)
console.log(a3);
var a4 = plus()(3)()()(2); // 返回 5
console.log(a4);
var a5 = plus(3, 2, 4, 5); // 可以接收多个参数
console.log(a5);
var a6 = plus(3)(2, 3, 5); // 同理
console.log(a6);
console.log("-----------------------------end01-------------------------------");
}
// 柯里化(Currying)情景二:
function addCurry(a, b) {
if (arguments.length < 1) {
return addCurry;
} else if (arguments.length < 2) {
return function(c) {
return a + c
}
} else {
return a + b;
}
}
function testCurry02(){
console.log("-----------------------------start02-------------------------------");
var myaddCurry = addCurry(2);
console.log(myaddCurry);
var total = myaddCurry(3);
console.log(total);
console.log("-----------------------------end02-------------------------------");
}
window.onload = function(){
testCurry01();
console.log("");
testCurry02();
}
// 柯里化(Currying)情景一:
// 第一个参数为要应用的function,第二个参数是需要传入的最少参数个数
function curry(func, outerArgs) {
if (outerArgs == undefined) {
outerArgs = 1;
}
function funcTempInvoke(innerArgs) {
return function () {
// 优化处理,如果调用时没有参数,返回该函数本身
var args = Array.prototype.slice.call(arguments);
var newArgs = innerArgs.concat(args);
if (newArgs.length >= outerArgs) {
return func.apply(this, newArgs);
} else {
return funcTempInvoke(newArgs);
}
};
}
return funcTempInvoke([]);
}
function testCurry01(){
//这里定义了一个插件式函数;
var plus = curry(function () {
var result = 0;
for (var i = 0; i < arguments.length; ++i) {
result += arguments[i];
}
return result;
}, 2);
console.log("-----------------------------start01-------------------------------");
var a1 = plus(3, 2); // 正常调用
console.log(a1);
var a2 = plus(3); // 偏应用,返回一个函数(返回值为3+参数值)
console.log(a2);
var a3 = plus(3)(2); // 完整应用(返回5)
console.log(a3);
var a4 = plus()(3)()()(2); // 返回 5
console.log(a4);
var a5 = plus(3, 2, 4, 5); // 可以接收多个参数
console.log(a5);
var a6 = plus(3)(2, 3, 5); // 同理
console.log(a6);
console.log("-----------------------------end01-------------------------------");
}
// 柯里化(Currying)情景二:
function addCurry(a, b) {
if (arguments.length < 1) {
return addCurry;
} else if (arguments.length < 2) {
return function(c) {
return a + c
}
} else {
return a + b;
}
}
function testCurry02(){
console.log("-----------------------------start02-------------------------------");
var myaddCurry = addCurry(2);
console.log(myaddCurry);
var total = myaddCurry(3);
console.log(total);
console.log("-----------------------------end02-------------------------------");
}
window.onload = function(){
testCurry01();
console.log("");
testCurry02();
}