• Stars
    star
    863
  • Rank 52,844 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 10 years ago
  • Updated about 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Lazy Object Streaming Pipeline for JavaScript

Stream.js Travic CI

ATTENTION: Stream.js is no longer maintained in favor of it's successor library Sequency. If you already use Stream.js in your project I recommend switching to Sequency. But don't worry, Stream.js still works fine so no hurry. 😉

Lazy Object Streaming Pipeline for JavaScript - inspired by the Java 8 Streams API

Stream(people)
   .filter({age: 23})
   .flatMap("children")
   .map("firstName")
   .distinct()
   .filter(/a.*/i)
   .join(", ");

Follow on Twitter for Updates

Getting started

Stream.js is a lightweight (2.6 KB minified, gzipped), intensely tested (700+ assertions, 97% coverage) functional programming library for operating upon collections of in-memory data. It requires EcmaScript 5+, has built-in support for ES6 features and works in all current browsers, Node.js and Java 8 Nashorn.

Download the latest release from GitHub or install Stream.js via NPM or Bower:

npm install streamjs

# or

bower install streamjs

Read the APIDOC

Examples

Before explaining how Stream.js works in detail, here's a few real world code samples.

Filter and sort a collection of persons, then group everything by age.

Stream(people)
   .filter({married: true, gender: 'male'})
   .sorted("lastName")
   .groupBy("age");

Filter and transform a collection of tweets to its text content, then limit the tweets to a maximum of 100 and partition the results into 10 tweets per page:

Stream(tweets)
   .filter(function (tweet) {
      return tweet.author !== me;
   })
   .map("text")
   .filter(/.*streamjs.*/i)
   .limit(100)
   .partitionBy(10);

Create an array of 100 odd random numbers between 1 and 1000:

Stream
   .generate(function() {
      return Math.floor(Math.random() * 1000) + 1;
   })
   .filter(function (num) {
      return num % 2 === 1;
   })
   .limit(100)
   .toArray();

Create an infinite iterator, generating an odd random number between 1 and 1000 on each call to next():

var iterator = Stream
   .generate(function() {
      return Math.floor(Math.random() * 1000) + 1;
   })
   .filter(function (num) {
      return num % 2 === 1;
   })
   .iterator();

iterator.next();
iterator.next();

Create an array of 100 parent objects, each holding an array of 10 children:

Stream
    .range(0, 100)
    .map(function (num) {
        return {
            parentId: num,
            type: 'parent',
            children: []
        };
    })
    .peek(function (parent) {
        parent.children = Stream
            .range(0, 10)
            .map(function (num) {
                return {
                    childId: num,
                    type: 'child',
                    parent: parent
                };
            })
            .toArray();
    })
    .toArray();

How Streams work

Stream.js defines a single function Stream to create new streams from different input collections like arrays, maps or number ranges:

Stream([1, 2, 3]);
Stream({a: 1, b: 2, c: 3});
Stream.of(1, 2, 3);
Stream.range(1, 4);

Streams are monadic types with a bunch of useful operations. Those functions can be chained one after another to make complex computations upon the input elements. Operations are either intermediate or terminal. Intermediate operations are lazy and return the stream itself to enable method chaining. Terminal operations return a single result (or nothing at all). Some terminal operations return a special monadic Optional type which is described later.

Streams are not limited to finite data sources like collections. You can also create infinite streams of elements by utilizing generator or iterator functions.

Stream.generate(function() {
   return 1337 * Math.random();
);
Stream.iterate(1, function (seed) {
   return seed * 2;
});

Why Stream.js?

What's so different between Stream.js and other functional libraries like Underscore.js?

Stream.js is built around a lazily evaluated operation pipeline. Instead of consecutively performing each operation on the whole input collection, objects are passed vertically and one by one upon the chain. Interim results will not be stored in internal collections (except for some stateful operations like sorted). Instead objects are directly piped into the resulting object as specified by the terminal operation. This results in minimized memory consumption and internal state.

Stream operations are lazily evaluated to avoid examining all of the input data when it's not necessary. Streams always perform the minimal amount of operations to gain results. E.g. in a filter - map - findFirst stream you don't have to filter and map the whole data. Instead map and findFirst are executed just one time before returning the single result. This results in increased performance when operation upon large amounts of input elements.

Stream([1, 2, 3, 4])
   .filter(function(num) {   // called twice
      return num % 2 === 0;
   })
   .map(function(even) {     // called once
      return "even" + even;
   })
   .findFirst();             // called once

API Documentation

The Stream.js API is described in detail here. For more information about Java 8 Streams I recommend reading the official Javadoc and this blog post.

A type definition for using Stream.js with Typescript is available here.

Contributing

Found a bug or missing feature? Please open an issue! Your feedback and help is highly appreciated. Please refer to the contributing rules before sending a pull request. I would also love to hear your feedback via Twitter.

Copyright and license

Created and copyright (c) 2014-2016 by Benjamin Winterberg.

Stream.js may be freely distributed under the MIT license.

More Repositories

1

java8-tutorial

Modern Java - A Guide to Java 8
Java
16,488
star
2

github-matrix-screensaver

The GitHub Matrix Screensaver for Mac OSX
JavaScript
554
star
3

spring-react-example

Isomorphic Spring Boot React.js Example
JavaScript
526
star
4

sequency

⚡️ Type-safe functional sequences for processing iterable data
TypeScript
338
star
5

mobx-logger

Log Mobx Actions, Reactions, Transactions and Computations
JavaScript
229
star
6

github-matrix

The GitHub Matrix
Java
206
star
7

spring-kotlin-react-demo

Demo Webapp using SpringBoot, Kotlin and React.js
JavaScript
186
star
8

expekt

BDD assertion library for Kotlin
Kotlin
172
star
9

react-samples

Just a bunch of React.js examples
JavaScript
77
star
10

nake

A simplified Make for Java 8 Nashorn
JavaScript
72
star
11

jest-teamcity-reporter

Teamcity Reporter for Jest Unittest Results
JavaScript
50
star
12

java8-explorer

Java 8 API Explorer
Java
49
star
13

RNTimerExample

📱 React Native + Mobx sample app
JavaScript
42
star
14

javadoc-reloaded

Proposal for JEP 225: Javadoc Search
CSS
35
star
15

kotlin-examples

Kotlin code samples
Kotlin
15
star
16

algorithms

Various Algorithms written in ES6
JavaScript
11
star
17

challenge

Programming challenges
Kotlin
10
star
18

chaya

An HTML5 Web Chat
JavaScript
9
star
19

dotfiles

My personal dotfiles for Mac OSX terminal
Shell
7
star
20

chrome-bookmarks

Extension for Google Chrome to show your bookmarks in a compact popup
JavaScript
4
star
21

android-moneysheet

Money Sheet for Android
Java
3
star
22

compary

A library for composing complex compare-functions in JavaScript
TypeScript
2
star
23

sway

TypeScript
1
star
24

FoodTracker

Sample iOS App written in Swift
Swift
1
star
25

langs

Learning new programming languages
Swift
1
star