SonOfLilit
5/30/2007 1:47:00 PM
Sorry, found a bug, I'll need to make it into this:
module Enumerable
def serially(&b)
a = []
self.each{|x| t = ([x].instance_eval(&b); a << b[0] unless b.empty?}
# notice, relevance to "it" discussion that's currently going on, just btw
end
end
On 5/30/07, SonOfLilit <sonoflilit@gmail.com> 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}
>
> 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.
>
> 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.
>
> Aur
>
>