Streams for JavaScript, also known as lazy sequences.
Support: NodeJS, IE9+.
License: MIT
Download with npm
:
npm install continuous-stream
Then require
in your project:
var Continuous = require('./continuous.js');
// Constructor for chaining
var Stream = Continuous.Stream;
// Functions for composition
var StreamI = Continuous.StreamI;
<script src="continuous.js"></script>
<script>
// Now you can use Stream and StreamI
</script>
Continuous can be used with chaining:
var result = new Stream(1,2,3,4,5)
.filter(function(x){return x > 3})
.map(function(x){return x * 2})
.toArray();
And composition:
// Add functions to global scope
// using an "extend" helper
$.extend(window, StreamI);
var result = toArray(
map(function(x){return x * 2},
filter(function(x){return x > 3},
stream(1,2,3,4,5))));
Continuous works on strings as well:
var result = new Stream('hello')
.map(function(x){return x.toUpperCase()})
.toArray()
.join('');
StreamI: head, tail, stream, toStream, iterate, map, pluck, filter, unique, reject, without, take, takeWhile, takeWhere, drop, dropWhile, zipWith, interleave, fold, fold1, toArray, toObject, doStream, each, find, some, every, append, union, accumulate, join, flatMap, repeat, ints, rand, chars, memo.
First item
new Stream(1,2,3).head(); //=> 1
Eveything but the first item
new Stream(1,2,3).tail(); //=> thunk (2 3)
# function only
Create a stream
stream(1,2,3); //=> thunk (1 2 3)
# function only
Get a stream from an array
toStream([1,2,3]); //=> thunk (1 2 3)
# function only
Takes a function and an accumulator to generate an infinite stream.
ones = iterate(function(x){return x}, 1);
ones(); //=> thunk (1 1 1 ...)
Apply a function to every item of the stream
new Stream(1,2,3).map(function(x){return x + 1}); //=> thunk (2 3 4)
Create a new stream of property values from a collection
var people = [
{name: 'Peter'},
{name: 'Mike'}
];
new Stream(toStream(people)).pluck('name'); //=> thunk ('Peter' 'Mike')
Keep items that pass the test
new Stream(1,2,3).filter(function(x){return x % 2 !== 0}); //=> thunk (1 3)
Remove duplicates from the stream
new Stream(1,1,2,2,3,3).unique(); //=> thunk (1 2 3)
Remove items that pass the test
new Stream(1,2,3).reject(function(x){return x < 2}); //=> thunk (2 3)
Remove the given items
new Stream(1,2,3,4).without(2,3); //=> thunk (1 4)
Take n items
new Stream(1,2,3).take(2); //=> thunk (1 2)
Take items while they pass the test
new Stream(1,2,3).takeWhile(function(x){return x < 3}); //=> thunk (1 2)
Take items where property and value match
var people = [
{name: 'Peter', age: 24},
{name: 'Mike', age: 15}
{name: 'Mike', age: 42}
];
new Stream(toStream(people)).takeWhere({name: 'Mike'});
//^ thunk ({name: 'Mike', age: 15} {name: 'Mike', age: 15})
Remove n items from the head of the stream
new Stream(1,2,3).drop(2); //=> thunk (3)
Drop items until the condition is met
new Stream(1,2,3).dropWhile(function(x){return x < 2}); //=> thunk (3)
Merge two streams with function
new Stream(1,2,3).zipWith(function(x,y){return x + y}, stream(4,5,6));
//^ thunk (5 7 9)
Merge two streams lazily by interleaving their items. Useful for infinite lazy streams.
new Stream(ints(0)).interleave(chars('a','z')); //=> thunk ('a' 0 'b' 1 'c' 2 ...)
Reduce items in the stream with a function and an accumulator
new Stream(1,2,3).fold(0, function(acc,x){return acc + x}); //=> 6
Same as fold but where the accumulator is the first item
new Stream(1,2,3).fold(function(x,y){return x + y}); //=> 6
Process the stream into an array
new Stream(1,2,3).toArray(); //=> [1, 2, 3]
Process the stream into an object
new Stream('a',1,'b',2,'c',3).toObject(); //=> {a:1, b:2, c:3}
Process the stream up to n items to do side effects
new Stream(1,2,3).doStream(2, function(x){console.log(x)});
//> 1 .. 2
Same as doStream
but til Infinity
. Do not use with infinite streams unless you take
items first.
new Stream(1,2,3).each(function(x){console.log(x)});
//> 1 .. 2 .. 3
Return the index of the item if found otherwise return false
new Stream(1,2,3).find(2); //=> 1
new Stream(1,2,3).find(5); //=> false
Check if at least one item passes the test
new Stream(1,2,3).some(function(x){return x > 0}); //=> true
Check if all items pass the test
new Stream(1,2,3).every(function(x){return x < 3}); //=> false
Append a stream to another stream
new Stream(1,2,3).append(stream(4,5,6)); //=> thunk (1 2 3 4 5 6)
Append only unique items
new Stream(1,2,3).append(stream(2,3,4)); //=> thunk (1 2 3 4)
Fold a stream of streams with a function that operates on streams
streamOfStreams = new Stream(stream(stream(1, 2), stream(3, 4)));
streamOfStreams.accumulate(interleave); //=> thunk (1 3 2 4)
Accumulates a stream of streams by interleaving items
streamOfStreams = new Stream(stream(stream(1, 2), stream(3, 4)));
streamOfStreams.join(); //=> thunk (1 3 2 4)
# function only
Monadic "bind"
var result = toArray(
flatMap(stream(1,2), function(x) {
return flatMap(stream(3,4), function(y) {
return stream(x + y);
});
});
);
//^ [4,5,5,6]
Get stream out of the class container
# method only
toArray(new Stream(1,2,3).get()); //=> [1, 2, 3]
Clone a stream at any point in the chain
# method only
var stream = new Stream(1,2,3);
var newStream = stream.clone().drop(1).toArray(); //=> [2, 3]
Repeat item infinitely
repeat(1); //=> thunk (1 1 1 ...)
Infinite stream of integers from n
ints(5); //=> thunk (5 6 7 ...)
Infinite stream of random numbers between 0 and 1
rand(); //=> thunk (0.12321313576, 0.87603421337, 0.91267844482 ...)
Infinite stream of characters from start to end
chars('a', 'c'); //=> thunk ('a' 'b' 'c' 'a' 'b' 'c' ...)
Continuous doesn't memoize by default but you can use memo
if you need to:
memo(fibonacciStream);