continuous-stream

0.9.3 • Public • Published

Continuous.js

Streams for JavaScript, also known as lazy sequences.

Support: NodeJS, IE9+.
License: MIT

Install

Node and Browserify:

Download with npm:

npm install continuous-stream

Then require in your project:

var Continuous = require('./continuous.js');

// Constructor for chaining
var Stream = Continuous.Stream;

// Functions for composition
var StreamI = Continuous.StreamI;

Browser:

<script src="continuous.js"></script>
<script>
// Now you can use Stream and StreamI
</script>

How to

Continuous can be used with chaining:

var result = new Stream(1,2,3,4,5)
  .filter(function(x){return x > 3})
  .map(function(x){return x * 2})
  .toArray();

And composition:

// Add functions to global scope
// using an "extend" helper
$.extend(window, StreamI);

var result = toArray(
  map(function(x){return x * 2},
  filter(function(x){return x > 3},  
  stream(1,2,3,4,5))));

Continuous works on strings as well:

var result = new Stream('hello')
  .map(function(x){return x.toUpperCase()})
  .toArray()
  .join('');

Functions/Methods

StreamI: head, tail, stream, toStream, iterate, map, pluck, filter, unique, reject, without, take, takeWhile, takeWhere, drop, dropWhile, zipWith, interleave, fold, fold1, toArray, toObject, doStream, each, find, some, every, append, union, accumulate, join, flatMap, repeat, ints, rand, chars, memo.

head

First item

new Stream(1,2,3).head(); //=> 1

tail

Eveything but the first item

new Stream(1,2,3).tail(); //=> thunk (2 3)

stream

# function only

Create a stream

stream(1,2,3); //=> thunk (1 2 3)

toStream

# function only

Get a stream from an array

toStream([1,2,3]); //=> thunk (1 2 3)

iterate

# function only

Takes a function and an accumulator to generate an infinite stream.

ones = iterate(function(x){return x}, 1);
ones(); //=> thunk (1 1 1 ...)

map

Apply a function to every item of the stream

new Stream(1,2,3).map(function(x){return x + 1}); //=> thunk (2 3 4)

pluck

Create a new stream of property values from a collection

var people = [
  {name: 'Peter'},
  {name: 'Mike'}
];

new Stream(toStream(people)).pluck('name'); //=> thunk ('Peter' 'Mike')

filter

Keep items that pass the test

new Stream(1,2,3).filter(function(x){return x % 2 !== 0}); //=> thunk (1 3)

unique

Remove duplicates from the stream

new Stream(1,1,2,2,3,3).unique(); //=> thunk (1 2 3)

reject

Remove items that pass the test

new Stream(1,2,3).reject(function(x){return x < 2}); //=> thunk (2 3)

without

Remove the given items

new Stream(1,2,3,4).without(2,3); //=> thunk (1 4)

take

Take n items

new Stream(1,2,3).take(2); //=> thunk (1 2)

takeWhile

Take items while they pass the test

new Stream(1,2,3).takeWhile(function(x){return x < 3}); //=> thunk (1 2)

takeWhere

Take items where property and value match

var people = [
  {name: 'Peter', age: 24},
  {name: 'Mike', age: 15}
  {name: 'Mike', age: 42}
];

new Stream(toStream(people)).takeWhere({name: 'Mike'});
//^ thunk ({name: 'Mike', age: 15} {name: 'Mike', age: 15})

drop

Remove n items from the head of the stream

new Stream(1,2,3).drop(2); //=> thunk (3)

dropWhile

Drop items until the condition is met

new Stream(1,2,3).dropWhile(function(x){return x < 2}); //=> thunk (3)

zipWith

Merge two streams with function

new Stream(1,2,3).zipWith(function(x,y){return x + y}, stream(4,5,6));
//^ thunk (5 7 9)

interleave

Merge two streams lazily by interleaving their items. Useful for infinite lazy streams.

new Stream(ints(0)).interleave(chars('a','z')); //=> thunk ('a' 0 'b' 1 'c' 2 ...)

fold

Reduce items in the stream with a function and an accumulator

new Stream(1,2,3).fold(0, function(acc,x){return acc + x}); //=> 6

fold1

Same as fold but where the accumulator is the first item

new Stream(1,2,3).fold(function(x,y){return x + y}); //=> 6

toArray

Process the stream into an array

new Stream(1,2,3).toArray(); //=> [1, 2, 3]

toObject

Process the stream into an object

new Stream('a',1,'b',2,'c',3).toObject(); //=> {a:1, b:2, c:3}

doStream

Process the stream up to n items to do side effects

new Stream(1,2,3).doStream(2, function(x){console.log(x)});
//> 1 .. 2

each

Same as doStream but til Infinity. Do not use with infinite streams unless you take items first.

new Stream(1,2,3).each(function(x){console.log(x)});
//> 1 .. 2 .. 3

find

Return the index of the item if found otherwise return false

new Stream(1,2,3).find(2); //=> 1
new Stream(1,2,3).find(5); //=> false

some

Check if at least one item passes the test

new Stream(1,2,3).some(function(x){return x > 0}); //=> true

every

Check if all items pass the test

new Stream(1,2,3).every(function(x){return x < 3}); //=> false

append

Append a stream to another stream

new Stream(1,2,3).append(stream(4,5,6)); //=> thunk (1 2 3 4 5 6)

union

Append only unique items

new Stream(1,2,3).append(stream(2,3,4)); //=> thunk (1 2 3 4)

accumulate

Fold a stream of streams with a function that operates on streams

streamOfStreams = new Stream(stream(stream(1, 2), stream(3, 4)));
streamOfStreams.accumulate(interleave); //=> thunk (1 3 2 4)

join

Accumulates a stream of streams by interleaving items

streamOfStreams = new Stream(stream(stream(1, 2), stream(3, 4)));
streamOfStreams.join(); //=> thunk (1 3 2 4)

flatMap

# function only

Monadic "bind"

var result = toArray(
  flatMap(stream(1,2), function(x) {
    return flatMap(stream(3,4), function(y) {
      return stream(x + y);
    });
  });
);
//^ [4,5,5,6]

get

Get stream out of the class container

# method only

toArray(new Stream(1,2,3).get()); //=> [1, 2, 3]

clone

Clone a stream at any point in the chain

# method only

var stream = new Stream(1,2,3);
var newStream = stream.clone().drop(1).toArray(); //=> [2, 3]

Extra

repeat

Repeat item infinitely

repeat(1); //=> thunk (1 1 1 ...)

ints

Infinite stream of integers from n

ints(5); //=> thunk (5 6 7 ...)

rand

Infinite stream of random numbers between 0 and 1

rand(); //=> thunk (0.12321313576, 0.87603421337, 0.91267844482 ...)

chars

Infinite stream of characters from start to end

chars('a', 'c'); //=> thunk ('a' 'b' 'c' 'a' 'b' 'c' ...)

memo

Continuous doesn't memoize by default but you can use memo if you need to:

memo(fibonacciStream);

smiley

Readme

Keywords

Package Sidebar

Install

npm i continuous-stream

Weekly Downloads

2

Version

0.9.3

License

none

Last publish

Collaborators

  • elclanrs