All Projects → winterbe → Streamjs

winterbe / Streamjs

Licence: mit
Lazy Object Streaming Pipeline for JavaScript

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Streamjs

godsend
A simple and eloquent workflow for streaming messages to micro-services.
Stars: ✭ 15 (-98.25%)
Mutual labels:  stream, streaming-api
Jiosaavnapi
An unofficial API for JioSaavn written in Python 3
Stars: ✭ 123 (-85.66%)
Mutual labels:  streaming-api, stream
Cypher Stream
Neo4j Cypher queries as Node.js object streams
Stars: ✭ 58 (-93.24%)
Mutual labels:  streaming-api, stream
rxjava2-http
Transmit RxJava2 Flowable over http with non-blocking backpressure
Stars: ✭ 19 (-97.79%)
Mutual labels:  stream, streaming-api
Mithril Data
A rich data model library for Mithril javascript framework
Stars: ✭ 17 (-98.02%)
Mutual labels:  stream
Bililiverecorder
B站录播姬 | BiliBili Stream Recorder
Stars: ✭ 746 (-13.05%)
Mutual labels:  stream
Grpc By Example Java
A collection of useful/essential gRPC Java Examples
Stars: ✭ 709 (-17.37%)
Mutual labels:  stream
Membrane core
The core of the Membrane Framework, advanced multimedia processing framework
Stars: ✭ 662 (-22.84%)
Mutual labels:  streaming-api
Trie
A Mixed Trie and Levenshtein distance implementation in Java for extremely fast prefix string searching and string similarity.
Stars: ✭ 25 (-97.09%)
Mutual labels:  stream
J8plus
Library containing useful tools for Java 8
Stars: ✭ 23 (-97.32%)
Mutual labels:  stream
Smux
A Stream Multiplexing Library for golang with least memory usage
Stars: ✭ 818 (-4.66%)
Mutual labels:  stream
Sonerezh
A self-hosted, web-based application to stream your music, everywhere.
Stars: ✭ 750 (-12.59%)
Mutual labels:  stream
Sql Streams
Painless low level jdbc abstraction using the java 8 stream api.
Stars: ✭ 17 (-98.02%)
Mutual labels:  stream
Homebridge Camera Ffmpeg
Homebridge Plugin Providing FFmpeg-based Camera Support
Stars: ✭ 726 (-15.38%)
Mutual labels:  stream
Redis Stream Demo
Demo for Redis Streams
Stars: ✭ 24 (-97.2%)
Mutual labels:  stream
Streamhut
Stream your terminal to web without installing anything 🌐
Stars: ✭ 676 (-21.21%)
Mutual labels:  stream
Springcloud Learning
Spring Cloud基础教程,持续连载更新中
Stars: ✭ 6,839 (+697.09%)
Mutual labels:  stream
Node Jl Sql Api
SQL for JS objects streams
Stars: ✭ 19 (-97.79%)
Mutual labels:  stream
Codebird Php
Easy access to the Twitter REST API, Direct Messages API, Account Activity API, TON (Object Nest) API and Twitter Ads API — all from one PHP library.
Stars: ✭ 773 (-9.91%)
Mutual labels:  streaming-api
Node Fetch
A light-weight module that brings the Fetch API to Node.js
Stars: ✭ 7,176 (+736.36%)
Mutual labels:  stream

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].