lazy.js~
9.12 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var stream = require('stream');
function Lazy(em, opts) {
if (!(this instanceof Lazy)) return new Lazy(em, opts);
EventEmitter.call(this);
var self = this;
self.once = function (name, f) {
self.on(name, function g () {
self.removeListener(name, g);
f.apply(this, arguments);
});
}
if (!opts) opts = {};
var dataName = opts.data || 'data';
var pipeName = opts.pipe || 'pipe';
var endName = opts.pipe || 'end';
if (pipeName != endName) {
var piped = false;
self.once(pipeName, function () { piped = true });
self.once(endName, function () {
if (!piped) self.emit(pipeName);
});
}
self.push = function (x) {
self.emit(dataName, x);
}
self.end = function () {
self.emit(endName);
}
if (em && em.on) {
em.on(endName, function () {
self.emit(endName);
});
self.on(pipeName, function () {
em.emit(pipeName);
});
// Check for v0.10 or Greater (Stream2 has Duplex type)
if (stream.Duplex && em instanceof(stream)) {
em.on('readable', function () {
var x = em.read();
self.emit(dataName, x);
});
} else {
// Old Stream1 or Event support
em.on(dataName, function (x) {
self.emit(dataName, x);
});
}
}
function newLazy (g, h, l) {
if (!g) {
g = function () {
return true;
};
}
if (!h) {
h = function (x) {
return x;
};
}
var lazy = new Lazy(null, opts, l);
self.on(dataName, function (x, y) {
if (g.call(lazy, x)) {
lazy.emit(dataName, h(x), y);
}
});
self.once(pipeName, function () {
lazy.emit(pipeName);
});
return lazy;
}
self.filter = function (f) {
return newLazy(function (x) {
return f(x);
});
}
self.forEach = function (f) {
return newLazy(function (x) {
f(x);
return true;
});
}
self.map = function (f) {
return newLazy(
function () { return true },
function (x) { return f(x) }
);
}
self.head = function (f) {
var lazy = newLazy();
lazy.on(dataName, function g (x) {
f(x)
lazy.removeListener(dataName, g)
})
}
self.tail = function () {
var skip = true;
return newLazy(function () {
if (skip) {
skip = false;
return false;
}
return true;
});
}
self.skip = function (n) {
return newLazy(function () {
if (n > 0) {
n--;
return false;
}
return true;
});
}
self.take = function (n) {
return newLazy(function () {
if (n == 0) self.emit(pipeName);
return n-- > 0;
});
}
self.takeWhile = function (f) {
var cond = true;
return newLazy(function (x) {
if (cond && f(x)) return true;
cond = false;
self.emit(pipeName);
return false;
});
}
self.foldr = function (op, i, f) {
var acc = i;
var lazy = newLazy();
lazy.on(dataName, function g (x) {
acc = op(x, acc);
});
lazy.once(pipeName, function () {
f(acc);
});
}
self.sum = function (f) {
return self.foldr(function (x, acc) { return x + acc }, 0, f);
}
self.product = function (f) {
return self.foldr(function (x, acc) { return x*acc }, 1, f);
}
self.join = function (f) {
var data = []
var lazy = newLazy(function (x) {
data.push(x);
return true;
});
lazy.once(pipeName, function () { f(data) });
return self;
}
self.bucket = function (init, f) {
var lazy = new Lazy(null, opts);
var yieldTo = function (x) {
lazy.emit(dataName, x);
};
var acc = init;
self.on(dataName, function (x) {
acc = f.call(yieldTo, acc, x);
});
self.once(pipeName, function () {
lazy.emit(pipeName);
});
// flush on end event
self.once(endName, function () {
var finalBuffer = mergeBuffers(acc);
if (finalBuffer) {
yieldTo(finalBuffer);
}
});
return lazy;
}
// Streams that use this should emit strings or buffers only
self.__defineGetter__('lines', function () {
return self.bucket([], function (chunkArray, chunk) {
var newline = '\n'.charCodeAt(0), lastNewLineIndex = 0;
if (typeof chunk === 'string') chunk = new Buffer(chunk);
for (var i = 0; i < chunk.length; i++) {
if (chunk[i] === newline) {
// If we have content from the current chunk to append to our buffers, do it.
if (i > 0) {
chunkArray.push(chunk.slice(lastNewLineIndex, i));
}
// Wrap all our buffers and emit it.
this(mergeBuffers(chunkArray));
lastNewLineIndex = i + 1;
}
}
if (lastNewLineIndex > 0) {
// New line found in the chunk, push the remaining part of the buffer.
if (lastNewLineIndex < chunk.length) {
chunkArray.push(chunk.slice(lastNewLineIndex));
}
} else {
// No new line found, push the whole buffer.
if (chunk.length) {
chunkArray.push(chunk);
}
}
return chunkArray;
});
});
}
Lazy.range = function () {
var args = arguments;
var step = 1;
var infinite = false;
if (args.length == 1 && typeof args[0] == 'number') {
var i = 0, j = args[0];
}
else if (args.length == 1 && typeof args[0] == 'string') { // 'start[,next]..[end]'
var arg = args[0];
var startOpen = false, endClosed = false;
if (arg[0] == '(' || arg[0] == '[') {
if (arg[0] == '(') startOpen = true;
arg = arg.slice(1);
}
if (arg.slice(-1) == ']') endClosed = true;
var parts = arg.split('..');
if (parts.length != 2)
throw new Error("single argument range takes 'start..' or 'start..end' or 'start,next..end'");
if (parts[1] == '') { // 'start..'
var i = parts[0];
infinite = true;
}
else { // 'start[,next]..end'
var progression = parts[0].split(',');
if (progression.length == 1) { // start..end
var i = parts[0], j = parts[1];
}
else { // 'start,next..end'
var i = progression[0], j = parts[1];
step = Math.abs(progression[1]-i);
}
}
i = parseInt(i, 10);
j = parseInt(j, 10);
if (startOpen) {
if (infinite || i < j) i++;
else i--;
}
if (endClosed) {
if (i < j) j++;
else j--;
}
}
else if (args.length == 2 || args.length == 3) { // start, end[, step]
var i = args[0], j = args[1];
if (args.length == 3) {
var step = args[2];
}
}
else {
throw new Error("range takes 1, 2 or 3 arguments");
}
var lazy = new Lazy;
var stopInfinite = false;
lazy.on('pipe', function () {
stopInfinite = true;
});
if (infinite) {
process.nextTick(function g () {
if (stopInfinite) return;
lazy.emit('data', i++);
process.nextTick(g);
});
}
else {
process.nextTick(function () {
if (i < j) {
for (; i<j; i+=step) {
lazy.emit('data', i)
}
}
else {
for (; i>j; i-=step) {
lazy.emit('data', i)
}
}
lazy.emit('end');
});
}
return lazy;
}
var mergeBuffers = function mergeBuffers(buffers) {
// We expect buffers to be a non-empty Array
if (!buffers || !Array.isArray(buffers) || !buffers.length) return;
var finalBufferLength, finalBuffer, currentBuffer, currentSize = 0;
// Sum all the buffers lengths
finalBufferLength = buffers.reduce(function(left, right) { return (left.length||left) + (right.length||right); }, 0);
finalBuffer = new Buffer(finalBufferLength);
while(buffers.length) {
currentBuffer = buffers.shift();
currentBuffer.copy(finalBuffer, currentSize);
currentSize += currentBuffer.length;
}
return finalBuffer;
}
util.inherits(Lazy, EventEmitter);
module.exports = Lazy;