柯里化是将多参数函数转换为单参数函数序列的技术,通过闭包递归收集参数,支持参数复用、延迟计算与函数组合,可扩展实现占位符机制提升灵活性。
柯里化(Currying)是函数式编程中的一种技术,它将使用多个参数的函数转换成一系列使用单个参数的函数。每次调用只传递一个参数,返回一个新的函数,直到所有参数都传齐后,执行原函数并返回结果。
实现一个简单的柯里化函数,核心思路是利用闭包和递归收集参数:
function curry(fn) {
return function curried(...args) {
if (args.length >= fn.length) {
return fn.apply(this, args);
} else {
return function(...nextArgs) {
return curried.apply(this, args.concat(nextArgs));
};
}
};
}
说明:
使用示例:
function add(a, b, c) {
return a + b + c;
}
const curriedAdd = curry(add);
console.log(curriedAdd(1)(2)(3)); // 6
console.log(curriedAdd(1, 2)(3)); // 6
console.log(curriedAdd(1)(2, 3)); // 6
有时希望跳过某个参数稍后填充,可以引入占位符机制(如 lodash 中的 `_`):
function advancedCurry(fn, placeholder = '_') {
return function curried(...args) {
const filledArgs = [];
let hasPlaceholder = false;
for (let i = 0; i < args.length; i++) {
if (args[i] === placeholder) {
hasPlaceholder = true;
break;
}
filledArgs.push(args[i]);
}
if (!hasPlaceholder && filledArgs.length >= fn.length) {
return fn.apply(this, filledArgs);
}
return function(...nextArgs) {
const combined = [];
let
nextIndex = 0;
for (let i = 0; i < args.length; i++) {
if (args[i] === placeholder && nextIndex < nextArgs.length) {
combined.push(nextArgs[nextIndex++]);
} else if (args[i] !== placeholder) {
combined.push(args[i]);
}
}
while (nextIndex < nextArgs.length) {
combined.push(nextArgs[nextIndex++]);
}
return curried.apply(this, combined);
};
};
}
这种实现允许你写类似 curriedFn(1, '_', 3)(2) 的调用方式。
log(level, msg) 可以柯里化出 errorLog = curryLog('error')
基本上就这些。柯里化让函数更灵活,但也要注意过度使用可能导致调试困难或性能损耗。