StreamJS v0.5.0

StreamJS is a very small (4.1kb minified) library that provides the power of streams for your computational needs.


StreamJS is small library that provides the power of streams for your computational needs.

Streams can provide all the functionalities of the ubiquitous list/array and some more oomph. This is because Streams offer an abstraction layer that simultaneously combines data and computation.

Getting Started

Get the latest release from Github or via npm/bower.

npm install stream-js
bower install stream-js

Creating Streams

Streams can be created using the constructor function or using the helper methods: from and fromArray.

var streamOf2 = new Stream(1, function () {
    return new Stream(2, null);

var stream = Stream.create(1,2,3);
var streamFromArray = Stream.fromArray([1,2,3]);

Using Streams

var s = Stream.create(1,2,3);
var sum = s.reduce(function(a, b) {
    return a + b;
var evenNaturals = NaturalNumbers().filter(function(val) {
    return val % 2 === 0;
var oddNaturals = NaturalNumbers.filter(function(val) {
    return val % 2 === 1;
var first100EvenNums = evenNaturals.pick(100);
var sum = first100EvenNums.sum();

More examples

1. A detailed blog post with more examples is available here.

2. Open your console and play with it!


Full documentation is available here.

Method Description
head() Returns the head of a Stream.
tail() Returns the tail of a Stream.
isEmpty() Checks if a Stream is empty.
hasEmptyTail() Checks if the tail of a Stream is empty.
append(s) Appends the stream s to the end of this stream.
pick(n) Picks the first n elements out of a stream, terminates when it gets to the nth item or reaches the end of the stream.
elementAt(index) Returns the element at index in a stream. Returns undefined if stream size is less than the index. Indexing is zero-based.
length() Gets the length of a stream - only defined for finite streams.
reduce(fn, initialValue) Reduces the stream to an accumulated value obtained over each stream element. Only valid for finite streams..
sum() Calculates the sum of the elements of a stream.
map(stream, fn) Maps a function over all the elements of a stream.
filter(fn) Filters a stream.
contains(element) Check if the stream contains element. Only defined for a finite stream.
walk(fn) Walks a stream and applies the input function to the stream. Stream has to be finite for this to work.
print(n) Prints out the first n elements of a stream, will stop if stream length is less than n.
remove(n) Removes the first n elements of a stream. Returns an empty stream if n is greater than the stream's length.
toArray() Constructs an array containing the elements of a finite stream.
add(s1, s2) Adds stream s1 to stream s2.
zip([...streams]) Zips all elements of the input streams together. Works like Python's zip but returns arrays of arrays instead of a list of tuples The nth entry of a zipped stream is an an array consisting of all the nth elements of its constituent streams.
create([...values]) Creates an input stream using a list of arguments.
fromArray(values) Creates an input stream from an array.
fromInterval(low, high)() constructs a stream made up of consecutive numbers in the range [low high].
from(start) Constructs an infinite stream of consecutive numbers starting from integer start.
upTo(stop) Constructs a stream made up of consecutive numbers up to stop.
Ones() Returns an infinite stream of ones.
NaturalNumbers() Returns the stream of Natural numbers.