Created
January 1, 2019 14:57
-
-
Save LH17/8ceccd83b816b621030d47785175a63a to your computer and use it in GitHub Desktop.
Iterator Design Pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct MyBestFilms: Sequence { | |
let films: [String] | |
func makeIterator() -> MyBestFilmsIterator { | |
return MyBestFilmsIterator(films) | |
} | |
} | |
struct MyBestFilmsIterator: IteratorProtocol { | |
var films: [String] | |
var cursor: Int = 0 | |
init(_ films: [String]) { | |
self.films = films | |
} | |
mutating func next() -> String? { | |
defer { cursor += 1 } | |
return films.count > cursor ? films[cursor] : nil | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment