stream8.js
Streams are an efficient, functional way to reduce boilerplate code for common array manipulation tasks. You'll start coding less, coding quicker and have code which is not only easier to read, but less prone to errors.
Installation
Stream8 is on bower with "stream8": "~0.2.0" or you can download the source from the app folder and or the minified version from dist.
What does it do?
I love examples.
Example 1: Calculate the average salary of people in the HR deparement. people is an array of objects like this {department:"myDepartment",salary:52000}
var averageSalary = people.stream()
.filter(function(val) { return val.department == 'HR'; }) // Find only people from HR
.map(function(val) { return val.salary; }) // Turn the stream into their salaries
.average(); // Average the salaries
Example 2: A common scenario is you may get some data via REST and you want to organize it so you can more quickly look up an object by a value. In this example I want to organize an array of things based on their IDs. things is an array of the following: [{id:23, name:'bob'}, {id:45, name:'jane'}, ...]
var thingMap = things.stream()
.collectFirst(function(val) { return val.id;}); // Make a map based on the id field of each object
Result:
{
23: {id:23, name:'bob'},
45: {id:45, name:'jane'},
...
}
With this map I can call thingMap[id] and get back the thing with that id. This approach reduces repetitively looping through a large array everytime I want to find a value.
Background
Arrays are great at storing data but not so great at performing computations. Stream8 offers a highly efficient way to perform operations on elements, re-organize into another array or calculate a value.
One key feature is the intermediate steps are done lazily. This means if I do something like this: array.stream().filter(crazyLongFunction).limit(0).toArray() that long running fuction will never be called and I'll immediately get an empty array back. You can think of a stream as a pipeline and each intermediate step will ask the previous step for a value only when it needs one. If you don't use Streams and instead use the built in array.filter (even if the browser supports array.filter!)... it'll happily call crazyLongFunction for every element in the array because the operations on the native array are performed eagerly.
Map-reduce operations based on Java8's Stream. Stream8 is a loose subset of these as it is tailored to the differences in Javascript such as weak typing and how 'this' is handled.