-
Notifications
You must be signed in to change notification settings - Fork 5
/
curry.js
117 lines (99 loc) · 2.44 KB
/
curry.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// 第一版柯里化函数
var curry = function(fn) {
var args = [].slice.call(arguments, 1);
return function() {
var newArgs = [].slice.call(arguments);
return fn.apply(this, args.concat(newArgs));
}
}
function add(a, b) {
return a+b;
}
var addCurry = curry(add, 1,2);
//第二版柯里化函数
function sub_curry(fn) {
var args = [].slice.call(arguments, 1);
return function() {
return fn.apply(this, args.concat([].slice.call(arguments)) );
};
}
function curry(fn, length) {
length = length || fn.length;
var slice = Array.prototype.slice;
return function () {
if(arguments.length < length) {
var combined = [fn].concat(slice.call(arguments));
return curry(sub_curry.apply(this, combined), length - arguments.length);
}else {
return fn.apply(this, arguments);
}
};
}
var fn = curry(function(a,b,c){
return a+b+c;
});
fn(1)(2,3); //6
fn(1)(2)(3); //6
fn(1,2,3); //6
//第三版柯里化函数实现
function curry(fn, args) {
var length = fn.length;
args = args || [];
return function() {
var _args = args.slice(0), arg,i;
for(i=0; i<arguments.length; i++) {
arg = arguments[i];
_args.push(arg);
}
if(_args.length < length) {
return curry.call(this, fn, _args);
}else {
return fn.apply(this, _args);
}
}
}
var fn = function add(a,b) {
return a+b;
}
var fnCurry = curry(fn);
fnCurry(3,4); //7
fnCurry(3)(4); //7
//简易版本
function curry(fn, args) {
var length = fn.length;
var args = args || [];
return function() {
var _args = args.slice(0);
console.log(_args);
for(var i=0;i<arguments.length;i++) {
_args.push(arguments[i]);
}
if(_args.length<length) {
return curry.call(this, fn, _args);
}else {
return fn.apply(this, _args);
}
}
}
var add = function(a,b) {
return a+b;
}
var addCurry = curry(add);
addCurry(3,4); //7
addCurry(3)(4); //7
//ES6版本
const curry = (fn,arr=[]) =>{
return (...args) => {
if([...arr, ...args].length === fn.length) {
return fn(...arr, ...args);
}else {
return curry(fn, [...arr, ...args]);
}
}
}
var add = function(a,b,c,d){
return a+b+c+d;
}
var addCurry = curry(add);
addCurry(1,2,3,4); //10
addCurry(1)(2)(3)(4); //10