-
Notifications
You must be signed in to change notification settings - Fork 65
/
stream.js
419 lines (375 loc) · 11.2 KB
/
stream.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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Stream = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
"use strict";
function Lazy(func) {
this.has_evaluated = false;
this.func = func;
this.value = null;
}
Lazy.prototype = {
eval: function() {
if ( this.has_evaluated ) {
return this.value;
}
this.value = this.func();
this.has_evaluated = true;
return this.value;
}
};
function Eager(func) {
this.func = func;
}
Eager.prototype = {
eval: function() {
return this.func();
}
}
function Stream( head, tailPromise, wrapper ) {
if ( typeof wrapper == 'undefined' ) {
wrapper = Lazy;
}
this.wrapper = wrapper;
if ( typeof head != 'undefined' ) {
this.headValue = head;
}
if ( typeof tailPromise == 'undefined' ) {
tailPromise = function () {
return this.create();
};
}
if ( typeof tailPromise == 'function' ) {
tailPromise = new wrapper(tailPromise);
}
this.tailPromise = tailPromise;
}
// TODO: write some unit tests
Stream.prototype = {
// This makes it easier to avoid passing around the explicit wrapper
// when making new streams below
create: function(head, tailPromise) {
return new Stream( head, tailPromise, this.wrapper );
},
empty: function() {
return typeof this.headValue == 'undefined';
},
head: function() {
if ( this.empty() ) {
throw new Error('Cannot get the head of the empty stream.');
}
return this.headValue;
},
tail: function() {
if ( this.empty() ) {
throw new Error('Cannot get the tail of the empty stream.');
}
return this.tailPromise.eval();
},
item: function( n ) {
if ( this.empty() ) {
throw new Error('Cannot use item() on an empty stream.');
}
var s = this;
while ( n != 0 ) {
--n;
try {
s = s.tail();
}
catch ( e ) {
throw new Error('Item index does not exist in stream.');
}
}
try {
return s.head();
}
catch ( e ) {
throw new Error('Item index does not exist in stream.');
}
},
length: function() {
// requires finite stream
var s = this;
var len = 0;
while ( !s.empty() ) {
++len;
s = s.tail();
}
return len;
},
add: function( s ) {
return this.zip( function ( x, y ) {
return x + y;
}, s );
},
append: function ( stream ) {
if ( this.empty() ) {
return stream;
}
var self = this;
return this.create(
self.head(),
function () {
return self.tail().append( stream );
}
);
},
zip: function( /* arguments */ ) {
var args = Array.prototype.slice.call( arguments, 0 )
var f = args[0];
args.shift();
var streams = [this].concat( args );
var self = this;
// If any streams are empty, return an empty stream
if( streams.filter( function(x) { return x.empty(); } ).length > 0 ) {
return new Stream();
}
return new Stream(
f.apply( null, streams.map( function(x) { return x.head(); }) ),
function () {
var tail = self.tail();
return tail.zip.apply( tail, [f].concat(
args.map( function (x) { return x.tail(); } )
) );
}
);
},
map: function( f ) {
if ( this.empty() ) {
return this;
}
var self = this;
return this.create( f( this.head() ), function () {
return self.tail().map( f );
} );
},
concatmap: function ( f ) {
return this.reduce( function ( a, x ) {
return a.append( f(x) );
}, this.create() );
},
reduce: function () {
var aggregator = arguments[0];
var initial, self;
if(arguments.length < 2) {
if(this.empty()) throw new TypeError("Array length is 0 and no second argument");
initial = this.head();
self = this.tail();
}
else {
initial = arguments[1];
self = this;
}
// requires finite stream
if ( self.empty() ) {
return initial;
}
// TODO: iterate
return self.tail().reduce( aggregator, aggregator( initial, self.head() ) );
},
sum: function () {
// requires finite stream
return this.reduce( function ( a, b ) {
return a + b;
}, 0 );
},
walk: function( f ) {
// requires finite stream
this.map( function ( x ) {
f( x );
return x;
} ).force();
},
force: function() {
// requires finite stream
var stream = this;
while ( !stream.empty() ) {
stream = stream.tail();
}
},
scale: function( factor ) {
return this.map( function ( x ) {
return factor * x;
} );
},
filter: function( f ) {
if ( this.empty() ) {
return this;
}
var h = this.head();
var t = this.tail();
if ( f( h ) ) {
return this.create( h, function () {
return t.filter( f );
} );
}
return t.filter( f );
},
take: function ( howmany ) {
if ( this.empty() ) {
return this;
}
if ( howmany == 0 ) {
return this.create();
}
var self = this;
return this.create(
this.head(),
function () {
return self.tail().take( howmany - 1 );
}
);
},
takeWhile: function( condition ) {
if(this.empty()) return new Stream();
var self = this, result = [];
while( condition( self.head() ) ) {
result.push( self.head() );
self = self.tail();
}
return Stream.fromArray(result);
},
drop: function( n ){
var self = this;
while ( n-- > 0 ) {
if ( self.empty() ) {
return this.create();
}
self = self.tail();
}
// create clone/a contructor which accepts a stream?
return this.create( self.headValue, self.tailPromise );
},
dropWhile: function( condition ) {
if(this.empty()) return new Stream();
var self = this;
while( condition( self.head() ) ) {
self = self.tail();
if ( self.empty() ) {
return self;
}
}
return new Stream( self.headValue, self.tailPromise );
},
member: function( x ){
var self = this;
while( !self.empty() ) {
if ( self.head() == x ) {
return true;
}
self = self.tail();
}
return false;
},
print: function( n ) {
var target;
if ( typeof n != 'undefined' ) {
target = this.take( n );
}
else {
// requires finite stream
target = this;
}
target.walk( function ( x ) {
console.log( x );
} );
},
toString: function() {
// requires finite stream
return '[stream head: ' + this.head() + '; tail: ' + this.tail() + ']';
}
};
function _continually( callback, wrapper ) {
return Stream.iterate( callback(), callback, wrapper );
}
Stream.continually = function( callback ) {
return _continually( callback, Lazy );
}
Stream.continuallyEager = function( callback ) {
return _continually( callback, Eager );
}
Stream.repeat = function( element ) {
return Stream.continually( function() { return element; } );
};
Stream.makeOnes = function() {
return new Stream( 1, Stream.makeOnes );
};
Stream.makeNaturalNumbers = function() {
return new Stream( 1, function () {
var nats = Stream.makeNaturalNumbers();
return nats.add( Stream.makeOnes() );
} );
};
Stream.make = function( /* arguments */ ) {
if ( arguments.length == 0 ) {
return new Stream();
}
var restArguments = Array.prototype.slice.call( arguments, 1 );
return new Stream( arguments[ 0 ], function () {
return Stream.make.apply( null, restArguments );
} );
};
// This is Eager-only, since keeping around a Lazy for each element would
// just copy array needlessly
Stream.fromArray = function ( array ) {
if ( array.length == 0 ) {
return new Stream();
}
return new Stream( array[0], function() { return Stream.fromArray(array.slice(1), Eager ); }, Eager );
};
Stream.range = function ( low, high ) {
if ( typeof low == 'undefined' ) {
low = 1;
}
if ( low == high ) {
return Stream.make( low );
}
// if high is undefined, there won't be an upper bound
return new Stream( low, function () {
return Stream.range( low + 1, high );
});
};
Stream.equals = function ( stream1, stream2 ) {
if ( ! (stream1 instanceof Stream) ) return false;
if ( ! (stream2 instanceof Stream) ) return false;
if ( stream1.empty() && stream2.empty() ) {
return true;
}
if ( stream1.empty() || stream2.empty() ) {
return false;
}
if ( stream1.head() === stream2.head() ) {
return Stream.equals( stream1.tail(), stream2.tail() );
}
};
function _iterate( x, f, wrapper ) {
return new Stream( x, function () {
return _iterate( f( x ), f, wrapper );
}, wrapper );
};
Stream.iterate = function( x, f ) {
return _iterate( x, f, Lazy );
}
Stream.iterateEager = function( x, f ) {
return _iterate( x, f, Eager );
}
// Like fromArray, this is Eager-only since it would needlessly copy
// parts of the array if Lazy was used
Stream.cycle = function( array ) {
var promise_generator = function( array, index) {
if( index >= array.length ) index = 0;
return function() {
return new Stream( array[index], promise_generator(array, index + 1 ), Eager );
};
};
return new Stream( array[0], promise_generator( array, 1 ), Eager );
};
/*
* Since Lazy and Eager aren't exported to the outside, this is used to make
* eager streams.
*/
function EagerStream( head, tailPromise ) {
return Stream.call( this, head, tailPromise, Eager );
}
EagerStream.prototype = Stream.prototype;
Stream.Eager = EagerStream;
module.exports = Stream;
},{}]},{},[1])(1)
});