From: Robert Feldt Date: 2001-03-16T18:05:14+09:00 Subject: [ruby-talk:12735] Re: Sum of Squares On Fri, 16 Mar 2001, Luigi Ballabio wrote: > To follow up on the Haskell-functional programming thing, this is where > [1,2,3].zip([4,5,6]) # --> [[1,4],[2,5],[3,6]] > and > [1,2,3].inject(0) { |a,b| a+b } # --> 1+2+3 = 6 > could come handy - the above would be > class Array > def dot(other) > self.zip(other).map { |a| a[0]*a[1] } .inject(0) { |a,b| a+b } > end > end > A good example of zip usage, indeed. Thanks for posting. If I remember correctly there was also a thread on how to sum the inter-element differences which would be ary.zip(ary.tail).map {|a,b| a-b} .inject(0) {|d,s| d+s} (And, yes we know that there are existing ways to do it... It's Turing complete remember ;-)) /Robert