-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprocedural-lambda.js
More file actions
271 lines (245 loc) · 7.44 KB
/
procedural-lambda.js
File metadata and controls
271 lines (245 loc) · 7.44 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/**
* @class
* A descriptor of a sequence of operations to be executed on a data source.
* @description
* Methods descriptors:
*
* Mutates state - changes the object's internal state
*
* Immutable - does not change the internal state, instead returns a new object
*
* Chainable - returns self, so chained calls are possible
*
* Quick - Passed function bodies will be injected into procedural code. Lambdas passed to _Quick_ methods *MUST* use the variable names `v,i,s` (all are optional; stands for value, index, self). Note that a Lambda does not use `{}`.
*
* Slow - Passed functions will be called from the procedural code.
*/
class ProceduralLambda {
/**
* @constructor
* @param {Iterable} [source] Attach to a data source. Can be defined later with `execute`.
*/
constructor(source){
this.source = source;
this.steps = [];
this.compiled = '';
}
/**
* Add a step to the processing pipeline.
* Mutates state. Chainable.
* @param {String} type A type of operation recognised by the compiler.
* @param {function} fn A user lambda.
* @param {...any} args Other arguments required for the function to be executed.
* @private
* @returns {ProceduralLambda}
*/
addStep(type, fn, ...args){
this.steps.push([type, fn].concat(args));
return this;
}
/**
* Copies another ProceduralLambda's pipeline.
* Mutates state. Chainable.
* @param {Array} steps The pipeline to copy.
* @private
* @returns {ProceduralLambda}
*/
withSteps(steps){
this.steps = steps.slice(0);
return this;
}
/**
* Add a filtering step.
* Quick. Immutable. Chainable
* @param {function(v,i,s): bool} lambda The Lambda to inject.
* @returns {ProceduralLambda} A new instance.
*/
filter(lambda){
return new ProceduralLambda(this.source)
.withSteps(this.steps)
.addStep('filter', lambda);
}
/**
* Add a filtering step.
* Slow. Immutable. Chainable.
* @param {function(v,i,s): bool} fn The function to call.
* @returns {ProceduralLambda} A new instance.
*/
filterComplex(fn){
return new ProceduralLambda(this.source)
.withSteps(this.steps)
.addStep('filter-fn', fn);
}
/**
* Add a map step.
* @param {function(v,i,s): *} lambda The Lambda to inject.
* @returns {ProceduralLambda} A new instance.
*/
map(lambda){
return new ProceduralLambda(this.source)
.withSteps(this.steps)
.addStep('map', lambda);
}
/**
* Add a map step.
* @param {function(v,i,s): *} fn The function to call.
* @returns {ProceduralLambda} A new instance.
*/
mapComplex(fn){
return new ProceduralLambda(this.source)
.withSteps(this.steps)
.addStep('map-fn', fn);
}
/**
* Add a reduce step.
* @param {function(accum,v,i,s): *} lambda The Lambda to inject.
* @param {*} init Initial _accum_ value.
* @returns {ProceduralLambda} A new instance.
*/
reduce(lambda, init){
return new ProceduralLambda(this.source)
.withSteps(this.steps)
.addStep('reduce', lambda, init);
}
/**
* Add a reduce step.
* @param {function(accum,v,i,s): *} fn The function to call.
* @param {*} init Initial _accum_ value.
* @returns {ProceduralLambda} A new instance.
*/
reduceComplex(fn, init){
return new ProceduralLambda(this.source)
.withSteps(this.steps)
.addStep('reduce-fn', fn, init);
}
/**
* Add a takeUntil step.
* When the passed lambda returns false no more rows are processed.
* @param {function(v,i,s): bool} lambda The Lambda to inject.
* @returns {ProceduralLambda} A new instance.
*/
takeUntil(lambda){
return new ProceduralLambda(this.source)
.withSteps(this.steps)
.addStep('take-until', lambda);
}
/**
* Add a takeUntil step.
* When the passed function returns false no more rows are processed.
* @param {function(v,i,s): bool} fn The Function to call.
* @returns {ProceduralLambda} A new instance.
*/
takeUntilComplex(fn){
return new ProceduralLambda(this.source)
.withSteps(this.steps)
.addStep('take-until-fn', fn);
}
/**
* Extract a Lambda function body. Does not work with regular function syntax.
* @param {function} lambda The Lambda to examine.
* @private
*/
static getLambdaBody(lambda){
return lambda.toString().split('=>')[1];
}
/**
* Extract the number of arguments a Lambda is accepting.
* @param {function} lambda The Lambda to examine.
* @private
*/
static getLambdaArgnum(lambda){
return lambda.toString().split('=>')[0].split(',').length;
}
/**
* Turn a pipeline of operations into procedural source code. Makes the next call to `execute` a bit faster.
* Mutates state.
*/
compile(){
const REDUCE_FUNCTIONS = ['reduce', 'reduce-fn'];
const is_reducing = (this.steps.filter(v => REDUCE_FUNCTIONS.indexOf(v[0]) !== -1).length > 0);
const INDICE_USERS = ['map', 'filter', 'take-until'];
const FUNCTIONS = ['map-fn', 'filter-fn', 'reduce-fn', 'take-until-fn'];
// TODO: Implement a check for regular functions too
// Indices are used if a lambda uses more than 1 argument, or if there are regular functions
const is_using_i = this.steps.filter(v =>
FUNCTIONS.indexOf(v[0]) !== -1 ||
(INDICE_USERS.indexOf(v[0]) !== -1 && ProceduralLambda.getLambdaArgnum(v[1]) > 1) ||
(REDUCE_FUNCTIONS.indexOf(v[0]) !== -1 && ProceduralLambda.getLambdaArgnum(v[1]) > 2)
).length > 0;
this.compiled = `
let index = 0;
let cursor = 0;
const lim = all.length;
let s = result;
${is_using_i ? `let per_step_i = [${Array.apply(null, new Array(this.steps.length)).map(_ => 0).join(',')}];` : ''}
${is_reducing ? 'let accum = ('+this.steps.filter(v => v[0] === 'reduce' || v[0] === 'reduce-fn')[0][2]+');' : ''}
for(index = 0; index < lim; index++){
let v = all[index], i${is_using_i ? '' : ' = index;'};
${this.steps.map((step,step_i,steps) => {
let line = '';
if(is_using_i)
line = `i = per_step_i[${step_i}]++;
`;
switch(step[0]){
case 'filter':
line += `if(!(${ProceduralLambda.getLambdaBody(step[1])}))continue;`;
break;
case 'map':
line += `v = ${ProceduralLambda.getLambdaBody(step[1])};`;
break;
case 'filter-fn':
line += `if(!this.steps[${step_i}][1](v,i,s))continue;`;
break;
case 'map-fn':
line += `v = this.steps[${step_i}][1](v,i,s);`;
break;
case 'reduce':
line += `accum = (${ProceduralLambda.getLambdaBody(step[1])});continue;`;
break;
case 'reduce-fn':
line += `accum = this.steps[${step_i}][1](accum,v,i,s);continue;`;
break;
case 'take-until':
line += `if(!(${ProceduralLambda.getLambdaBody(step[1])}))break;`
break;
case 'take-until-fn':
line += `if(!this.steps[${step_i}][1](v,i,s))break;`;
break;
default: throw Error('Invalid step '+step[0]);
}
/*line += `
per_step_i[${step_i}]++`;*/
return line;
}).join(`
`)}
${!is_reducing ? 'result[cursor++] = v;' : ''}
}
${is_reducing ? 'result = accum;' : 'result.splice(cursor)'}
`;
return this;
}
/**
* Execute the processing pipeline on a data source.
* @param {Iterable} [source] The data source to examine.
* @returns {Array|*} The result of the pipeline, which is an array or a value if the pipeline uses `reduce`.
*/
execute(source){
const all = source || this.source;
let result = new Array(all.length);
if(!this.compiled)
this.compile();
try {
eval(this.compiled);
}catch(err){
throw Error(`Compiled Lambda kernel failed.\n${this.compiled}\n${err.toString()}`);
}
return result;
}
/**
* Return the result length. Provided for compatibility with `Array` interface.
*/
get length(){
return this.execute().length;
}
}
module.exports = ProceduralLambda;