> >note that turning the above into Common Lisp's "map" function should > >be fairly easy. (about 8 more chars.) > def collect2(&p) > x=[] > collect {|a| > a.length > }.min.times {|i| > x << yield(collect {|a| a[i]}) > } > x > end note that the "(&p)" is useless because the proc parameter is accessed directly by yield regardless of it being declared. I usually write something like p.call instead of yield, but I mixed the two here. =/ > The code between each2 and collect2 being so similar, out of curiosity, i > tried toying around to see if it could be generalized in Ruby. Something > like: > [a1,a2,a3, ...].loop_n (:collect} {...} > [a1,a2,a3, ...].loop_n (:each} {...} > [a1,a2,a3, ...].loop_n (:min} {...} > etc > would like to do it concisely as: > f( yield(collect {|a| a[i]}) ) The best I could do like that is: class Array def transpose_2d smallest = collect {|a| a.length }.min r = [] smallest.times {|i| r << collect {|a| a[i] } } r end def loop_n(method_sym,&proc) transpose_2d.method(method_sym).call(&proc) end end The problem with it is that it makes (the equivalent) of a copy of the original arrays, and that renders bang-methods useless. Memory-wise it can be a hog. Speed-wise it's quite acceptable (unlike builtin Array#uniq) :-) > but i'm continuously amazed at the genius of McCarthy in the design of the > Cambridge prefix notation for Lisp. Granted, its an acquired taste and with > experience the parens fade into the background, but one gains enormous > uniformity: The point of putting the verb second, and the subject first, as in foo.collect{|x|...}, is because of an other enormous uniformity that is even more enormous. This is a (reasonable) trade-off. The drawbacks show up when you wish to have several subjects, like above. matju