Is there any way to work with a Node stream as an iterable? <p>I’m writing a tool which processes a bunch of text passed in to <code>stdin</code>, each line is an “entry”. I’d like to make my code more functional, so I’d like to treat the set of lines as a “sequence” or “iterable” and iterate over it using <code>reduce</code>.</p> <p>I’m currently using the <a href="http://nodejs.org/" rel="nofollow">Node</a> module <a href="https://github.com/shinout/LineStream" rel="nofollow">LineStream</a> to process <code>stdin</code> as a set of lines, but it works by dispatching a <code>data</code> event for each line — which is fine, it’s implementing the <a href="http://nodejs.org/docs/latest/api/streams.html#readable_Stream" rel="nofollow">Readable Stream</a> interface.</p> <p>So I’m currently doing kind of a very “manual” reduce by passing the interim value in to my function every time the <code>data</code> event fires:</p> <pre><code>var windows = []; linestream.on('data', function(line) { return windows = rollup(windows, extractDate(line), argv.w); }); linestream.on('end', function() { return process.stdout.write(toCsv(windows)); }); process.stdin.resume(); </code></pre> <p>But it’d be more functional to do something like:</p> <pre><code>linestream.lines.reduce(rollup, []); function rollup(windows, line) { // would return a new interim or final value } </code></pre> <p>Of course, I could “collect” all the lines into a regular array and then reduce it, but I tried that and it uses way too much memory when I’m running my tool on a large dataset — so something like iteration over the stream is really what’s necessary.</p> <p>I guess what I’m asking is whether it’s possible to write a Node function/module which would do this, or whether one exists already.</p> <p>Thanks!</p>