From: Brad Phelan Date: 2007-05-31T03:25:26+09:00 Subject: Re: Enumerable#serially - those nifty functions w/o memory footprint Brad Phelan wrote: > Brad Phelan wrote: >> Brian Candler wrote: >>> On Wed, May 30, 2007 at 10:46:33PM +0900, SonOfLilit wrote: >>>> 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 >>> >>> Somehow I don't think you tested this :-) >>> >>> * You have mismatched parentheses >>> * You assign to t, but never use the value >>> * you call b twice, once with no arguments, and once with 0 as an >>> argument >>> * b is a block, but you call #empty? on it >>> >>> Can you give an example of how this is supposed to be used? >>> >>> Brian. >>> >> >> I think I get his point. I wrote a little lib to try out my >> interpretation of what he is trying to do. >> >> >> a = (1..20) >> >> b = a.serially do >> select do |x| >> if x > 10 >> true >> else >> false >> end >> end >> collect do |x| >> "0x" + x.to_s >> end >> select do |x| >> if x == "0x15" >> false >> else >> true >> end >> end >> end >> >> puts b >> >> >> >> # generates >> >> 0x11 >> 0x12 >> 0x13 >> 0x14 >> 0x16 >> 0x17 >> 0x18 >> 0x19 >> 0x20 >> >> The lib is at >> >> http://xtargets.com/snippets/posts/show/69 >> >> -- >> Brad Phelan >> http://xtargets.com/snippets >> > > A quick profile with ruby-prof find the above technique about 10 times > slower in 50000 elements than for the traditional method. > > -- > Brad Phelan > http://xtargets.com This little bit of code has got me interested. Turns out I can create a version with comparable time execution by slicing the the input enumerable into memory friendly chunks and then performing all operations on those chunks. This kind of reminds me of C++ expression templating tricks See the link http://xtargets.com/snippets/posts/show/69 for the latest highspeed version and the ruby-prof results -- Brad Phelan http://xtargets.com