From: raja@... (Raja S.) Date: 2000-12-10T03:40:01+09:00 Subject: [ruby-talk:6980] Re: parallel array traversal raja> Is there a way to do a parallel traversal of a group of arrays in Ruby? raja> Or more generally move a collection of index variables in lock-step? dave> It ain't pretty, but dave> http://www.rubycentral.com/faq/rubyfaq-5.html#ss5.5 Thanks for the pointer. Interesting. raja> but was wondering if there was something along the lines of Common Lisp's raja> 'loop' where x & y move in tandem: raja> raja> (loop for x in '(1 3 5 7 9) raja> for y in '(1 9 25 49 81) raja> do (format t "~& ~d ~d" x y)) dave> What does this do if x and y were iterating over different length dave> arrays? Stops when it reaches the end of the shortest sequence. Python's 'zip' has the same semantics: python>>> zip([1],[0,2,4]) python>>> [(1, 0)] matz> IIRC, Python's zip creates [[a1, b1, c1], [a2, b2, c2],...] from matz> [a1,a2,...], [b1, b2,...] and [c1, c2,...]. Correct. matz> How about something like matz> [a1, a2, a3].zip do |x, y, z| matz> ... matz> end matz> which do not create unnecessary objects? There might be better name matz> for this method. Seems reasonable. The semantics issue of: (1) do we stop iterating once the smallest segment ends (Common Lisp, Python) (2) iterate for the longest segment and pad the smaller ones with nil (???) (3) consider it an error for the sequences to be of different sizes (Scheme) I'd go for option (1) The name is also an important issue. #foreach is a possibility (used in Dir and IO though with a different intention) and would be similar to #each. Regards, Raja