From: Tomasz Wegrzanowski Date: 2006-08-21T20:03:44+09:00 Subject: Re: You can write Fortran in any language On 8/21/06, Bil Kleb wrote: > Here is an example used for computing correlations. > It's almost a direct translation of several hundred > lines of Fortran 77. > > If you can bare to look, please help, even it is simply > to say "yo bonehead, why didn't you just require 'some_library'?" [cut] > for i in 0..x.size-1 Well, it's a very small thing, but tripple-dot makes is somewhat more readable: for i in 0...x.size And I'd actually use x.size.times {|i| }, for loops looks kinda alien in Ruby code. Now pieces of code like: sum_x = Array.new(x.size){0.0} sum_x_sq = Array.new(x.size){0.0} for i in 0..x.size-1 sum_x[i] = x[i].inject(0) { |sum, e| sum + e } sum_x_sq[i] = x[i].inject(0) { |sum, e| sum + e**2 } end can be rewritten to: sum_x = (0...x.size).map{|i| x[i].inject(0) { |sum, e| sum + e }} sum_x_sq = (0...x.size).map{|i| x[i].inject(0) { |sum, e| sum + e**2 }} which looks a lot neater imho. It would look even neater to write: sum_x = x.map{|xi| xi.sum} sum_x_sj = x.map{|xi| xi.map{|e|e**2}.sum} but Ruby doesn't have Enumerable#sum by default. Oh well, you have a few ideas of making the code nicer now :-) -- Tomasz Wegrzanowski [ http://t-a-w.blogspot.com/ ]