From: Joel VanderWerf Date: 2003-04-02T09:45:05+09:00 Subject: Re: Iterate over two lists in parallel Johan Holmberg wrote: > Gavin Sinclair writes: > >>I'd like someone, hopefully including Julian and Jim (Weirich), to >>demonstrate how to iterate over twop lists in parallel. Extra marks >>for setting a realistic example problem and solving it! >> >>Gavin >> > > > Maybe this is off-topic, > but I can't see anyone mentioning the 'enum' package by Joel VanderWerf. > > Here is a solution to "Iterate over two lists in parallel" > using that package: > > #------------------------------ > require 'enum/op' > include EnumerableOperator > > alist = [11, 22, 33, 44] > blist = ["a", "b", "c", "d"] > clist = 1001..1004 > > for a,b,c in diagonal(alist,blist,clist) > puts "got #{a}, #{b} and #{c}" > end > > #------------------------------ > > /Johan Holmberg > It's kind of a cheat, though. Unlike the solutions using continuations, this diagonal thing actually converts the inputs to arrays, if they aren't already. So if you want to do parallel enumeration of infinite sequences, such as PRNGs, you never even get the first tuple. OTOH, for small sequences, the overhead of converting to arrays (if necessary) might be less than the overhead of instantiating continuations. IIRC, continuations are on the same order of magnitude as threads, in that respect.