[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: LFM + LFSP = LFE?




On Thursday, June 12, 2003, at 02:29 PM, Avi Bryant wrote:

> In Smalltalk you'd use something like
>
> #(1 2 3) with: #(4 5 6) collect: [:a :b | a * b]

Ruby would probably be something like:

class Array
   def collect_with(a) # yes... you'd need to extend Array
     r=[]
     self.each_index { |i| r << yield(self[i], a[i]) }
     r
   end
   alias map_with collect_with # to stay consistent w/ module Enumerable
end

a = (1..3).to_a
b = (4..6).to_a
a.collect_with(b) { |n, m| n * m }

Not nearly as elegant as scheme... but it works. I'm sure there is 
probably 3 different ways to write this better but it's 2.5 in the 
mornin'.