The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Appending lazy lists

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Florian Frank

Posts: 48
Nickname: ffrank
Registered: Dec, 2005

Florian Frank is a humanoid life-form, living on the third planet of the solar system.
Appending lazy lists Posted: Dec 23, 2005 5:46 PM
Reply to this message Reply

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.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Florian Frank
Latest Posts From The Rubylution

Advertisement

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.

require 'lazylist'
require 'zlib'
files = [ 'syslog' ] + Dir['syslog.[0-9].gz']
files.map! { |f|
  LazyList[f =~ /.gz$/ ? Zlib::GzipReader.new(File.new(f)) : File.new(f)]
}

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.

Read: Appending lazy lists

Topic: Thesis finished Previous Topic   Next Topic Topic: New Computer

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use