swiftsequencemultipass

What is a multi-pass Sequence in Swift?


I am looking at this language from the Swift GeneratorType documentation and I'm having a hard time understanding it:

Any code that uses multiple generators (or for...in loops) over a single sequence should have static knowledge that the specific sequence is multi-pass, either because its concrete type is known or because it is constrained to CollectionType. Also, the generators must be obtained by distinct calls to the sequence's generate() method, rather than by copying.

What does it mean for a sequence to be "multi-pass"? This language seems quite important, but I can't find a good explanation for it. I understand, for example, the concept of a "multi-pass compiler", but I'm unsure if the concepts are similar or related...

Also, I have searched SO for other posts that answer this question. I have found this one, which makes the following statement in the C++ context:

The difference between algorithms that copy their iterators and those that do not is that the former are termed "multipass" algorithms, and require their iterator type to satisfy ForwardIterator, while the latter are single-pass and only require InputIterator.

But the meaning of that isn't entirely clear to me either, and I'm not sure if the concept is the same in Swift.

Any insight from those wiser than me would be much appreciated.


Solution

  • A "multi-pass" sequence is one that can be iterated over multiple times via a for...in loop or by using any number of generators (constructed via generate())

    The text explains you would know a sequence is multi-pass because you