From: Mauricio Fernandez Date: 2006-07-05T19:32:30+09:00 Subject: Re: Parallel Array On Wed, Jul 05, 2006 at 11:36:31AM +0900, Gregory Brown wrote: > On 7/4/06, Matthew Smillie wrote: > > ># using inject is fun. Let's do it again. > >c = (0...a.length).inject([]) { |x, n| x << a[n] + b[n] } > > better would be c = (0...a.length).inject([]) { |x, n| x + [ a[n] + b[n] ] } ugh this is doubly slow. #inject, an extra literal array and an Array#+ for each pair, making it O(n**2)... If you absolutely want to feel smart for using #inject, a = [1,2,3,4] b = [1,1,2,2] c = a.inject([[],0]){|(arr, i),e| [arr << e+b[i], i + 1]}.first c # => [2, 3, 5, 6] ... but it's still silly. Either use the Array initializer, manual aref in a a.size.times block, or look into narray. -- Mauricio Fernandez - http://eigenclass.org - singular Ruby