Here you go
var n = 0 var naturalNumbers = anyGenerator{return n++} naturalNumbers.next() naturalNumbers.next() naturalNumbers.next() naturalNumbers.next() // and enum Dwarf: Int { case Bashful = 1, Doc, Dopey, Grumpy, Happy, Sleepy, Sneezy static var members : [Dwarf] { var i = 1 return Array(anyGenerator{return Dwarf(rawValue: i++)}) } } print(Dwarf.members)
4 Comments
Any idea why this has been made into a class in Swift 2?
I think it was because of the immutable state that is implied by a struct – since anyGenerator is so stateful it seems more class-ish. The reason that the standard library generators aren’t classes, then, is because of speed requirements, as far as I know.
I think you can even drop the “return” statement.
as in “var naturalNumbers = anyGenerator{n++}
“