Zip with length checking

Consider a loop

for (i, elt) in zip(indexes, itr)
    do_something(i, elt)
end

where Base.IteratorSize(itr) can potentially be Base.HasLength() or Base.SizeUnknown(). I want the code to work for both.

indexes is an AbstractVector so I know its length.

I want to check that the iteration does not terminate “early” because itr is shorter than `indexes. What’s the idiomatic way to do so?

I thought of the following:

  1. manually iterate itr using,
  2. use a counter and check that.

But neither seems elegant.

zip(indices, takestrict(itr, length(indices))) from Itertools would be a choice. Actually, I might add zipstrict to Itertools.

1 Like