From: zuzu Date: 2004-08-27T06:23:09+09:00 Subject: Re: python generators to ruby closures, help On Fri, 27 Aug 2004 05:20:54 +0900, Martin DeMello wrote: > > > Michael Neumann wrote: > > zuzu wrote: > > > can anyone help me decode the following python example using > > > generators into The Ruby Way using closures? > > > > I'm sure the Ruby version must be human-readable... maybe you can > > describe in short words, what the Python example tries to achive? > > > > You might have a look at generator.rb which ships with Ruby and > > implements generators or "streams" with continuations. > > And possibly at http://redshift.sourceforge.net/enum/pipe.html > > martin > > this looks quite straight-forward, thank you. as with Generator, i've long suspected this would involve some inherit tweaking of Enumerable, and all the implementations of this genre seem to do just that. in the quote below, this is basically "lazy evaluation", right? (which i think is "by definition" with asynchronous continuations rather than synchronous returns.) Using #pipe has potential performance advantages. The iteration e.collect { |x| x.m }.each { |y| ... } can be rewritten as e.pipe(:m).each { |y| ... } which doesn't generate an intermediate array, and uses a send instead of a proc call. Of course, it could also be written as e.each { |x| y = x.m ... } but that may be undesirable for readability or because the block is to be taken from a proc contained in a variable: pr = proc { ... } e.pipe(:m).each &pr