An simple lazy evaluated stream API in Javascript inspired by Java 8 stream
npm install lazy-streamjs
You can use the global object stream
to create your stream:
Get a new stream of numbers in the range [from, to[
Get a new stream from a function generatorFunction
called everytime a new element is needed
Get a Stream from your collection (Array, Object or String) passing it to stream
function:
Apply intermediate operations to the stream by just concatenating them:
Map all elements using mapFunction
Take only elements that satisfy the predicate filterFunction
Take first n
elements of the stream
Apply foldl using foldlFunction
and base
as base case
Apply foldr using foldrFunction
and base
as base case
You can get the result using:
Will get back the next element of the stream or null
at the end
Will get back all the elements of the stream
var stream = require('lazy-streamjs');
var data = [-2, -100, 2, 1, 2, 3, 4];
var result = stream(data)
.map(function(x) { return x + 1; })
.filter(function(x) { return x > 0; })
.getAll();
console.log(result);
Will print
3 2 3 4 5
var stream = require('lazy-streamjs');
var fibGenerator = (function() {
var e1 = 0,
e2 = 1;
return function() {
var next = e1 + e2;
e1 = e2;
e2 = next;
return next;
}
})();
res = Stream.function(fibGenerator)
.take(10)
.getAll();
Will print the first 10 elements of the Fibonacci Series (will not cause an infinite loop because of it's lazy evaluated)