chain-stream
Chain stream operations together
Example
var chain =fromArray = fromArray// double them// count them// map the result into three values// map them to streams// flatten the streams into one stream// filter for multiples of 5// sum them asynchronously but serially to conserve sum.value {// 470console}
Installation
npm install chain-stream
Contents
Chain stream gives you transformations and consumption functions over streams. They also give you different types like sync / async vs serial / parallel.
About half of these are implemented.
Types
There are different types of iterators. They are useful and can be combined
chain.method
(Sync version)chain.methodAsync
chain.methodSerial
Sync
The iterator is synchronous. i.e. it is finished after the function returns
// sync mapthing
All transformation and consumption functions are sync by default.
Async
The iterator is asynchronous. i.e. it is finished some time later when the callback function is called
thing
This async iterator will run your callbacks in parallel
Serial
All Serial iterators are also async, sync iterators run in serial by defualt.
A serial iterator is one where the next value cannot be iterated over before the current iterator finishes
thing
Note that if there were 10 items in thing this would take 5s where as the parallel version takes 500ms.
However serial is useful if you want to preserve the order like doing asynchronous mapping over a file and you want to preserve the original order of lines.
Transformation functions
Transformations take a stream and return a stream with the transformation queued up.
All transformations will be applied lazily only once the stream is consumed
Map
map takes an iterator and replaces every value by the result of the iterator.
var doubles = thing
Filter
filter takes an iterator and keeps the value if the iterator returns true
var odds = things
Remove
remove takes an iterator and drops the value if the iterator returns true
var evens = things
Reductions
reductions takes an iterator and an accumalator. It replaces the value and accumulator with the returned value
var sums = things
Reductions may need a combining function if it's asynchronous so that it knows how to combine the accumulators.
var sums = things
Take
take returns a stream containing only the first n elements
var firstTen = things.take(10)
drop
drop returns a streaming that doesn't contain the first n elements
var restButTen = things.drop(10)
Take While
take while returns a stream containing the first n elements while the iterator returns true
var sensible = things.takeWhile(isSensible)
take while is serial by default. Parallel doesn't make much sense
Drop while
drop while returns a stream with the first n elements dropped while the iterator returns true
var withoutFirstSensible = things.dropWhile(isSesnsible)
drop while is serial by default. Parallel doesn't make much sense
Flatten
flatten flattens out all the items if the items are an array or a stream
var items = lists
Concat Map
concat map is a mapping followed by a flatten
var values = things// values = things.map(iterator).flatten()
Concat
concat takes multiple things and turns them into a single list and then flattens that.
var numbers =// list([events, odds]).flatten()
reduce
call the iterator with the current accumulator and the value for each item.
things.reduce(function (acc, v) {
/* ... */
}, initial)
This will need a combination function as well if it's asynchronous.
things.reduce(function reduction(acc, v, cb) {
/* do asynchronous reduction */
}, initial)
reduce returns a stream containing one value
every
Returns the first value that fails the predicate or true
things.every(somePredicate)
some
Returns the first value that matches the predicate or false
things.some(somePredicate)
lazyPipe
Lazyily pipe a stream onto this. It's a way to say
I want to pipe this stream but not now
things
.lazyPipe(someStream)
.moreChainingThings()
first
Returns a stream containing the first value
var s = things.first()
Last
Returns a stream containing the last value
var s = things.last()
Consumption functions
A consumption function starts consumign items in the stream
forEach
call the iterator for each item
things.forEach(console.log)
toArray
turn the stream into an array. This obvouisly buffers the entire stream into an array
things.toArray(function (array){
...
})
value
Return the last chunk in the array
data.value(function (lastChunk) {
...
})
Contributors
- Raynos