0.1.0 • Published 8 years ago

lazy-streamjs v0.1.0

Weekly downloads
1
License
ISC
Repository
github
Last release
8 years ago

npm version

Stream.js

An simple lazy evaluated stream API in Javascript inspired by Java 8 stream

Installation

npm install lazy-streamjs

API

Initial operations

You can use the global object stream to create your stream:

stream.range(from, to)

Get a new stream of numbers in the range [from, to[

stream.function(generatorFunction)

Get a new stream from a function generatorFunction called everytime a new element is needed

stream(data);

Get a Stream from your collection (Array, Object or String) passing it to stream function:

Intermediate operations

Apply intermediate operations to the stream by just concatenating them:

.map(mapFunction)

Map all elements using mapFunction

.filter(filterFunction)

Take only elements that satisfy the predicate filterFunction

.take(n)

Take first n elements of the stream

.foldl(foldlFunction, base)

Apply foldl using foldlFunction and base as base case

.foldr(foldrFunction, base)

Apply foldr using foldrFunction and base as base case

Final operations

You can get the result using:

.getNext()

Will get back the next element of the stream or null at the end

.getAll()

Will get back all the elements of the stream

Example

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)

0.1.0

8 years ago

0.0.8

8 years ago

0.0.6

9 years ago

0.0.5

9 years ago

0.0.4

9 years ago

0.0.3

9 years ago

0.0.2

9 years ago

0.0.1

9 years ago