forked from dreamapplehappy/hacking-with-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurry-2016-7-19.js
99 lines (88 loc) · 2.92 KB
/
curry-2016-7-19.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
// showMsg
function showMsg(name, age, gender) {
var msg = 'My name is ' + name + ' ,my age is ' + age + ' ,my gender is ' + gender;
console.log(msg);
return msg;
}
// 初级柯里化
curryHelper(showMsg)('dreamapple' ,22, 'man');
curryHelper(showMsg, 'dreamapple')(22, 'man');
curryHelper(showMsg, 'dreamapple', 22)('man');
curryHelper(showMsg, 'dreamapple', 22, 'man')();
betterCurry(showMsg)('dreamapple')(22)('man');
//betterCurry(showMsg)('dreamapple', 22)('man');
//betterCurry(showMsg)('dreamapple', 'man')(22);
// curryHelper
function curryHelper(fn) {
var args = Array.prototype.slice.call(arguments, 1);
return function() {
var newArgs = Array.prototype.slice.call(arguments);
var totalArgs = args.concat(newArgs);
return fn.apply(this, totalArgs);
}
}
// betterCurry
function betterCurry(fn, len) {
len = len || fn.length;
return function() {
var argsEqual = (arguments.length >= len);
if(argsEqual) {
return fn.apply(this, arguments);
}
var args = Array.prototype.slice.call(arguments);
var newArgs = [fn].concat(args);
return betterCurry(curryHelper.apply(this, newArgs), len - arguments.length);
}
}
// bestCurry
var k = 0;
function curry(fn, length, args, holes) {
length = length || fn.length;
args = args || [];
holes = holes || [];
return function() {
var _args = args.slice(),
_holes = holes.slice();
// Store the length of the args and holes received
var argLength = _args.length,
holeLength = _holes.length;
var allArgumentsSpecified = false;
// Loop vars
var arg = null,
i = 0,
aLength = arguments.length;
//console.log('xxxxxxxxxxxxxxx-->' + k++, aLength, holeLength, argLength, '参数长度!');
for(; i < aLength; i++) {
arg = arguments[i];
if(arg === _ && holeLength) {
holeLength--;
var _h = _holes.shift();
_holes.push(_h);
} else if (arg === _) {
_holes.push(argLength + i);
} else if (holeLength) {
holeLength--;
var _hc = _holes.shift();
_args.splice(_hc, 0, arg);
} else {
_args.push(arg);
}
}
allArgumentsSpecified = (_args.length >= length);
if(allArgumentsSpecified) {
console.log(_args, 'all');
return fn.apply(this, _args);
}
// keep currying
return curry.call(this, fn, length, _args, _holes);
};
}
function ar(a, b, c, d, e, f){}
var _ = {};
var a = curry(showMsg);
a('man')('dreamapple',_,_)(_,22,_);
a(_,22,_)(_,_,'man')('dreamapple',_,_);
//a('dreamapple',_,_)(_,22,_)(_,_,'man');
//a('dreamapple',22,_)(_,_,'man');
//a('dreamapple')(_,22)(_,_,'man');
//a(_,22)('dr')(_,_,'man');