From: Just Another Victim of the Ambient Morality Date: 2007-11-28T22:35:04+09:00 Subject: Re: Ruby needs continuations... "Austin Ziegler" wrote in message news:9e7db9110711280500j3d7b1c6byf36c6d769c05bcbd@mail.gmail.com... > On 11/28/07, Vitor Peres wrote: >> On Nov 28, 2007 10:30 AM, Just Another Victim of the Ambient Morality < >> ihatespam@hotmail.com> wrote: >> > How would one do this in Ruby? You can use .each_with_index and >> > index >> > the other list but that assumes that the other list is indexable, which >> > only happens to be true of arrays but is not true in general. I've >> > thought >> > about implementing some Ruby equivalent of zip() (or preferably izip()) >> > and >> > discovered that I can't do so without continuations. > > Actually, Ruby 1.9 is losing continuations (hard to do with native > threads, expensive for stack frame manipulation in JRuby). However, > you don't need continuations to do parallel iteration, as Vitor > suggests: > >> Ruby does, however, have an implementation of zip in Enumerable. You can >> do >> >> list1 = [1, 2, 3, 4] >> list2 = ['a', 'b', 'c', 'd'] >> >> list1.zip(list2).each {|num, letter| puts "#{num} #{letter}" } >> >> just fine. You'll actually get the same result you would with your Python >> example. > > You're also able to do: > > a.zip(b, c, d, ...) > > to iterate multiple lists in parallel. Thank you, both, for pointing this out! Considering my failure to create such a method, I was about to ask how such a thing was implemented but then I just tried it out and I've discovered how: arrays. I've already mentioned that not all containers are indexable: linked lists are not and trees are not efficiently. Continuations allow even non-indexable containers to be iterated in parallel...