3

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.

Community
  • 1
  • 1
Aaron Rasmussen
  • 13,082
  • 3
  • 42
  • 43

1 Answers1

3

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

  • know its type (perhaps a class you designed) or
  • know it conforms to CollectionType. (for example, sets and arrays)
nielsbot
  • 15,922
  • 4
  • 48
  • 73
  • 1
    Thank you...I guess I'm having a hard time with visualizing a sequence that is *not* multipass. What would be an example of a sequence that you could only iterate over once? What would stop you from starting over at the beginning? – Aaron Rasmussen Jan 21 '15 at 01:28
  • 2
    Maybe it's a stream of tokens you're receiving from the network? All it has to be is something where each new item is generated from the previous one in a nondeterministic fashion... – nielsbot Jan 21 '15 at 03:44
  • Thanks again. I hadn't thought of a stream as being a sequence, and that helped me quite a bit. – Aaron Rasmussen Jan 22 '15 at 23:18