Sometimes I would find it useful to ‘zip’ or ‘mesh’ two or more iterators in a way that each iterator runs in turn, and once one is exhausted it continues to use the remaining ones. For example, when I want to search through integers (both positive and negative). For non-exhausting iterators, this can be achieved with
using Base.Iterators
for n in flatten(zip(countfrom(0), countfrom(-1,-1)))
...
# break if solution found
end
However, if iterators have different (finite) lengths then this will not work (once one of the iterators is exhausted it stops). Is there a clever trick how this can be achieved with stuff in Base.Iterators
and IterTools
? Or is it worth discussing adding this to IterTools
(would mesh
be a good name for this)?
So mesh(1:5, 13:18, 22:23)
would yield 1 13 22 2 14 23 3 15 4 16 5 17 18 in turn. If the given iterators are finite, the length of the ‘mesh’ iterator is the sum of the individual lengths. The type of the elements it generates is the union of the individual types.