Skip to content

Commit 0c5e651

Browse files
committed
feat: 函数柯里化
1 parent f5492ca commit 0c5e651

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

Cute-Gist/Function/Curry.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function curry (func) { // func 需要柯里化的函数
2+
return function curried (...args) {
3+
// func.length 柯里化函数的参数数量
4+
// args.length 返回函数的参数数量
5+
if(args.length >= func.length){
6+
return func.apply(this, args);
7+
}else{
8+
// 返回一个方法,用来接收其他参数
9+
return function(...args2){
10+
return curried.apply(this, args.concat(args2));
11+
}
12+
}
13+
}
14+
}
15+
const abc = function (a, b, c) {
16+
return [a, b, c];
17+
};
18+
19+
const curried = curry(abc);
20+
21+
console.log(curried(1)(2)(3)); // => [1, 2, 3]
22+
console.log(curried(1, 2)(3)); // => [1, 2, 3]
23+
console.log(curried(1, 2, 3)); // => [1, 2, 3]

0 commit comments

Comments
 (0)