-
Notifications
You must be signed in to change notification settings - Fork 3
/
Function.js
215 lines (184 loc) · 5.35 KB
/
Function.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* Look into hocf.html for more information */
Function.prototype.callarg = function(context/*, arg1, ...*/) {
var fn = this,
args = arguments;
return function() {
return Function.prototype.call.apply(fn, args);
};
};
/* Function.prototype.bind */
/* Function.prototype. = function() {
} */
Function.prototype.argwith = function(/*arg1, ...*/) {
var fn = this,
args = arguments;
return function(context) {
return fn.apply(context, args);
};
};
Function.prototype.argcall = function(/*arg1, ...*/) {
var fn = this,
args = Array.prototype.slice.call(arguments);
return function(context/*, argM, ...*/) {
return fn.apply(context, args.concat(Array.prototype.slice.call(arguments, 1)));
};
};
/* Function.prototype. = function() {
}; */
Function.prototype.arg = function(/*arg1, ...*/) {
var fn = this,
args = arguments;
return function() {
return fn.apply(this, args);
};
};
Function.prototype.pcall = Function.prototype.partial = function() {
if (arguments.length < 1)
return this;
var fn = this,
args = Array.prototype.slice.call(arguments);
return function(/*argM, ...*/) {
return fn.apply(this, args.concat(Array.prototype.slice.call(arguments)));
};
};
Object.keys(Function.prototype).concat(["bind"]).forEach(function(method) {
Function[method] = function(fn) {
if (typeof fn == "function")
return Function.prototype.bind.apply(Function.prototype[method], arguments);
return Function.prototype.argwith(Function.prototype[method], arguments);
};
});
Function.prototype.curry = function curry(length, context) {
if (typeof this != "function")
throw new TypeError();
if (typeof length != "number") {
if (typeof context == "undefined")
context = length;
length = this.length; // arguments.caller.length - Anzahl von entgegengenommen Argumenten
}
};
Function.prototype.xBind = function xBind(context/*, arg1, undefined, arg3, ...*/) {
};
Function.prototype.xPcall = function xPcall(/*arg1, undefined, arg3, ...*/) {
};
Function.prototype.xCurry = function xCurry(length, context) {
if (typeof this != "function")
throw new TypeError();
if (typeof length != "number") {
if (typeof context == "undefined")
context = length;
length = this.length; // arguments.caller.length - Anzahl von entgegengenommen Argumenten
}
};
Function.prototype.methodize = function methodize(context) {
var fn = this;
return function() {
var args = Array.prototype.slice.call(arguments, 0);
args.unshift(this);
return fn.apply(context || null, args);
};
};
Function.prototype.result = function result(r) {
var fn = this;
return function() {
fn.apply(this, arguments);
return r;
};
};
Function.prototype.bool = function(real) {
/* get: boolean real: whether the result should be returned as it is or get inverted
return: the functions result converted to boolean, optionally inverted */
var fn = this;
if (typeof real != "boolean")
real = true;
return function() {
return real === !!fn.apply(this, arguments);
};
};
Function.ident = Function.identity = Function.I = function identity(x) {
return x;
};
Function.const = function(x) {
return function() {
return x;
};
};
Function.chain = Function.compose = function compose(fn) {
var fns = Array.prototype.slice.call(arguments, 0);
if (fns.length == 0)
return Function.identity;
if (fns.length == 1)
return fn;
return function composed() {
// from http://base2.googlecode.com/svn/version/1.0.2/src/base2.js
var i = fns.length,
result = fns[--i].apply(this, arguments);
while (i--)
result = fns[i].call(this, result);
return result;
};
};
Function.prototype.nest = function nest(n) {
var fn = this;
if (typeof n == "number")
return function nested(x) {
for (var i=n; i--; )
x = fn.call(this, x);
return x;
};
else if (typeof n == "function")
return function nested(x) {
while (n(x))
x = fn.call(this, x);
return x;
};
else
// TODO: warning
return Function.identity;
}
Function.Named = function NamedFunction(name, args, body, scope, values) {
if (typeof args == "string")
values = scope, scope = body, body = args, args = [];
if (!Array.isArray(scope) || !Array.isArray(values)) {
if (typeof scope == "object")
values = Object.values(scope), scope = Object.keys(scope);
else
values = [], scope = [];
}
return Function(scope, "function "+name+"("+args.join(", ")+") {\n"+body+"\n}\nreturn "+name+";").apply(null, values);
};
Function.invoke = function(fn) {
var args = Array.prototype.slice.call(arguments, 1);
if (typeof fn == "function")
return fn.argWith.apply(fn, args); // function(obj) { return fn.apply(obj, args); }
else
return function(obj) {
return obj[fn].apply(obj, args);
};
};
/* https://github.com/fantasyland/fantasy-land */
// Functor
Function.prototype.map = function map(g) {
// Function.compose(g, this)
var f = this;
return function() {
return g(f.apply(this, arguments));
};
};
// Applicative
Function.prototype.ap = function ap(g) {
var f = this;
return function() {
return f.apply(this, arguments)(g.apply(this, arguments));
};
};
// Applicative, Monad
Function.prototype.of = Function.of = Function.const;
// Chain, Monad
// f.chain(g).chain(h) == ()=>h(g(f.apply()).apply()).apply() == ()=>g(f.apply()).chain(h).apply() == f.chain((x)=>g(x).chain(h))
Function.prototype.chain = function chain(g) {
var f = this;
return function() {
return g(f.apply(this, arguments)).apply(this, arguments);
};
};