This post originated from an RSS feed registered with Ruby Buzz
by Florian Frank.
Original Post: Appending lazy lists
Feed Title: The Rubylution
Feed URL: http://rubylution.ping.de/xml/rss/feed.xml
Feed Description: The Rubylution is a weblog (mainly) about Ruby Programming.
I've just added a nice new feature to my lazylist library in Version 0.2.2. It's now possible to append one list to another.
A good example of how this can be useful is reading log files:
First I put a bunch of file names into the files array. Then I wrap them into opened File objects and the gzipped files into GzipReader objects as well, after that all of them into LazyList instances.
Then I append all the lazy lists into one new lazy list.
syslog = files.first.append(*files[1..-1])
I grep all lines containing error from
this lazy list.
errors = syslog.grep /error/i
Now I can use this
puts errors.take(2)
to output the first two errors. (This takes a while.)
puts errors.take(3)
This takes a while and the first three errors are printed, but the first two have alread been read from the logfiles before.
puts errors.take(3)
This outputs those three errors again, but they are already in memory by now, and are displayed instantly.
puts errors.size
This greps for all the error lines in the syslog files and displays how many there were. This could take a while, because all files are read into memory.
puts *errors
This displays all the error lines.
The nice thing about appending is that several different log files can be treated like they were only one file - in an uniform way.
BTW: I aliased + to append, so it's possible to add them like ordinary Ruby Arrays:
l = LazyList[1..3] + LazyList[21..23] # => [1,...]
Of course if one of the appended lists is infinite, the lists on the right hand side, will never be part of the resulting list.