On 30.05.2007 15:35, SonOfLilit wrote:
> Hello,
> 
> I've seen many people write things like:
> 
> (1..1000000).to_a. # anything that is a huge enumerable can work as an 
> example
>  collect{|x| num_appearances(x)}.select{|x|
> x.is_norwegian?}.collect{|x| x.paycheck}.each{|x| p x}

Just guessing what all this does, but there is of course the obvious 
solution:

some_large_enum.each do |x|
   x = num_appearances(x)
   p x.paycheck if x.norwegian?
end

> I almost wrote something like that myself today.
> 
> The problem, of course, is the huge memory footprint - collect,
> select, collect each creates a new temporary array to store the
> results in.

Yes, that's really an issue.

> Here's a solution to that problem, exchanging it for a bit of speed
> (anything that uses those methods could obviously live with that,
> otherwise another language or method would be used):
> 
> module Enumerable
>  def serially(&b)
>    self.collect{|x| *([x].instance_eval(&b)}
>  end
> end
> 
> Just beware not to use it with sort or any of the "!" methods or with
> sort or sort_by.

I am not exactly sure I understand what this is supposed to do.This 
isn't even compiling properly.

Regards

	robert