GeneratorType.next is a lie.

GeneratorType.next doesn't return the next thing. As soon as next is called, the thing it returns is the current thing, not the next thing.


C# is accurate here. C# calls GeneratorType IEnumerator. C# doesn't have optionals, so instead of


func next() -> Self.Element?

, it has

bool MoveNext() // bad caveman English, but accurate. Returns false where next() returns nil.
T Current { get; }


In Swift, our for loops do this:

while let iteration = generator.next() {loop body.}


They should look like this:

repeat {
  generator.iterate()
  guard let iteration = generator.iteration else {break}

  loop body.
}

Then, we'd have access to the current thing,

and

next()
next()
next()

could be represented by an actiual verb instead of the impudent mix of verb and noun we have now, emulating what is said by someone who has been dealing with people in a queue in front of them all day long, and is annoyed that they're not coming right up to the counter on their own. 😠 "NEXT!!!"


struct Generator: GeneratorType {
  init(limit: Int) {self.limit = limit}

  mutating func iterate() {
     iteration! += 1
  }

  var iteration: Int? = 0 {
     didSet {
        if iteration > limit {iteration = nil}
     }
  }

  private let limit: Int
}