On 6/3/07, Sergey Volkov <gm.vlkv / gmail.com> wrote: > ----- Original Message ----- > From: "Robert Dober" <robert.dober / gmail.com> > To: "ruby-talk ML" <ruby-talk / ruby-lang.org> > Sent: Sunday, June 03, 2007 6:38 PM > Subject: Re: [QUIZ] FizzBuzz (#126) > > > > Sergey it is your fault if I cannot sleep ;), is this more readable ? > relax, Robert, nobody hurt :) > > > module Enumerable > > def change(to, &blk) > > map{ |x| blk.call(x) ? to : x } > > end > > end # module Enumerable > > > > puts (1..100).change(:FizzBuzz){ |x| x%15 == 0 }.change(:Fizz){ |x| > > x%3 == 0 rescue false}.change(:Buzz){ |x| x%5 == 0 rescue false } > hard to make formal conclusions - there are no formal requirements, > nor context is well defined, I can share my own opinion and potential > concerns only: > > - yes, indeed, this solution is more readable, > compared to your Array-nested-tricky-indexed original solution; > - clear functional decomposition simplifies understanding, maintenance; > - it is easily expandable thanks to chain processing; I feel good ;) > btw: I prefer different name for Enumerable method, ex: substitute_if; agreed and alias to change_if then there will be a substitute/change_unless too. > > Not so attractive in this solution: > - run-time/memory inefficient: each item is generates 4 times; > - memory inefficient: generates and stores in memory 4*original array Good point a change_if! might have been better. > (ex: not well suited for 10**9 length array); Too bad ;) > - can not be adopted for stream processing (read - process - print); good point, but is that not the price to pay for functional decomposition? > - 'rescue' is for exceptional processing, should be avoided in normal > control flow; I gotta think about this, I tend to agree for begin rescue ... end the inline form seems too compact for that and I do not consider it control flow but rather a logical operator like expression or_if_that_does_not_work other but you do have a point. Thx for your thoughts AAMOF you make me think that the discussion of your solution with the recruiters is very important too. Cheers Robert -- You see things; and you say Why? But I dream things that never were; and I say Why not? -- George Bernard Shaw