Iterating over multiple collections in parallel
Fawzi Mohamed
fmohamed at mac.com
Sat Jul 5 03:25:52 PDT 2008
Iterating over many collection in parallel is very nice to have, what
would be nice to have with a cleaner syntax for it.
Aldor had it, and since then I always missed it.
It worked like this:
Iterator/loops can be declared like this:
for a in [1,2,3]
for b in [1,0,7,3]
for i in 1..
while (b<7)
repeat {
...
}
which mean follow *all* iterators in parallel, and execute ..., when
any iterator stop, stop everything
very clear, nice and flexible.
Other syntaxes look like a kludge with respect to it.
Python for example has generators/iterators, and builds derived
iterators one on the top of the other
for a in [1,2,3]
for i in 0..
repeat { ... }
becomes
for i,a in enumerate([1,2,3].iterator()):
...
C++ iterators can be used in parallel, because all the difficulty of
keeping the current position is moved to the iterator writer, but the
syntax is just plain ugly and writing efficient iterators for complex
structures can be painful.
Anyway leaving away that syntax, in D without changes one can already
have a reasonable syntax (even if not as nice).
Using fibers, as in the nice example of downs it should also be
possible to convert automatically an opApply in a generator, so
technically it should be feasible, even if 500 cycles per conetext
switch, might be too much for some applications (tight loops).
So in D one could have a something workable
foreach(a,b,i;collectLoop([1,2,3],[2,3,7],Range(1,infinity))){
...
}
where collectLoop is a variadic template function that converts opApply
to a generator and returns an object having a opApply that loops in
parallel.
Implementation of it is left as exercice to the reader ;-)
Fawzi
More information about the Digitalmars-d
mailing list