Created
June 27, 2018 04:17
-
-
Save H-Ghadirian/1bb39be50079dba40e581deb8787881d to your computer and use it in GitHub Desktop.
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 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