Search code examples
node.jsstreamreactive-programmingfrphighland.js

Print result from reduce in Highland.js


I written a simple script which should count line

var
  H = require('highland'),
  fs = require('fs'),
  split = require('split');

var lineStream = fs.createReadStream('data-samples/sample.log').pipe(split());

H('data', lineStream).reduce(0, function(count) {
  return count + 1;
}).each(console.log);

But for some reasons I see nothing in console. Documentation says about lazyness but each call should "invoke" the stream. How to fix the issue?

NB: It's a question about highland.js and not about a way to count lines


Solution

  • 'reduce' stream only emits the final value. you should provide _.nil as final value for Highland Stream. like this:

    lineStream.on('end', function() { s.write(H.nil) });
    

    Also Highland.js natively supports Node streams, so you don't need create event stream. use:

    H(lineStream)
    

    instead

    H('data', lineStream)