-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuncurrying.js
58 lines (51 loc) · 1.29 KB
/
uncurrying.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
//uncurrying
:: obj.func(arg1, arg2)
// uncurried into
:: func(obj, arg1, arg2)
// a common implement of uncurrying
var uncurrying= function () {
var _this = this;
return function () {
return _this.apply(arguments[0],[].slice.call(arguments,1));
}
};
// another implement
var uncurrying= function () {
var _this = this;
return function () {
return Function.prototype.call.apply(_this ,arguments);
}
};
// also uncurrying
var uncurrying=Function.prototype.bind.bind(Function.prototype.call);
// equals to
var uncurrying= function(){
return Function.prototype.bind.apply(Function.prototype.call)
}
//equals to
var uncurrying = function(){
var _this = this;
return function(){
_this.call(arguments)
}
}
// borrow built-ins
var toUpperCase = uncurrying(String.prototype.toUpperCase);
toUpperCase('abc')
// replace a callback method with a static func
['a', 'b'].map(toUpperCase)
// objects(array-like) can 'call' Array.prototype.x
var push = uncurrying(Array.prototype.push);
var a = {};
push(a, 10)
// -> 1
a
// -> Object {0: 10, length: 1}
var slice = uncurrying(Array.prototype.slice);
slice(a, 0, 1)
// -> [10]
var pop = uncurrying(Array.prototype.pop)
pop(a)
// -> 10
a
// -> Object {length: 0}