Skip to content

Instantly share code, notes, and snippets.

@H-Ghadirian
Created June 27, 2018 04:17
Show Gist options
  • Save H-Ghadirian/1bb39be50079dba40e581deb8787881d to your computer and use it in GitHub Desktop.
Save H-Ghadirian/1bb39be50079dba40e581deb8787881d to your computer and use it in GitHub Desktop.
struct Novella {
let name: String
}
struct Novellas {
let novellas: [Novella]
}
struct NovellasIterator: IteratorProtocol {
private var current = 0
private let novellas: [Novella]
init(novellas: [Novella]) {
self.novellas = novellas
}
mutating func next() -> Novella? {
defer { current += 1 }
return novellas.count > current ? novellas[current] : nil
}
}
extension Novellas: Sequence {
func makeIterator() -> NovellasIterator {
return NovellasIterator(novellas: novellas)
}
}
/*:
### Usage
*/
let greatNovellas = Novellas(novellas: [Novella(name: "The Mist")] )
for novella in greatNovellas {
print("I've read: \(novella)")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment