The first post was about dnode - the freestyle rpc library for node, the second was about optimist - the lightweight options parser for node.

This time I'll introduce you to one of my own modules called node-lazy - lazy lists module for node.

Basically you create a new lazy object, and pump data into it via data events (it's an event emitter). Then you can manipulate this data via chaining by using various functional programming methods.

Here is a quick example. Here we create a new lazy object and define a filter that returns only even integers, then we take just 5 elements, then we apply map on them, and finally we join (think of threads) the result in a list:

This code fragment takes output from iptables -L -n -v and converts it into a data structure for later use.

Here a new Lazy object is created from an existing stream - the iptables.stdout stream. Next, the special lines getter is called that splits the stream on \n char and converts it into a line-stream. Then this stream is mapped onto String constructor to convert it to a string. Next, first two lines are skipped via skip(2) and then all the other lines are converted into a data structure via map.

You can also create all kinds of ranges with node-lazy, including infinite ranges. Check this out, ranges.js:

Sponsor this blog series!

Doing a node.js company and want your ad to appear in the series? The ad will go out to 14,000 rss subscribers, 7,000 email subscribers, and it will get viewed by thousands of my blog visitors! Email me and we'll set it up!