Created
January 15, 2021 13:00
-
-
Save below/63b58aae2ad162e801967c9d42d9052d 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
// Do you like my endless iterator? | |
struct EndlessIterator { | |
var baseIterator: String.Iterator? | |
var start: String | |
init(_ start: String) { | |
self.start = start | |
} | |
mutating func next() -> Character { | |
var item = baseIterator?.next() | |
if item == nil { | |
baseIterator = start.makeIterator() | |
item = baseIterator?.next() | |
} | |
guard let result = item else { | |
fatalError("Endless iterator ended") | |
} | |
return result | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Source