Skip to content

Instantly share code, notes, and snippets.

@robtimp
Created May 11, 2018 21:11
Show Gist options
  • Save robtimp/df3bd937f2eed81e6d2cf771980fa397 to your computer and use it in GitHub Desktop.
Save robtimp/df3bd937f2eed81e6d2cf771980fa397 to your computer and use it in GitHub Desktop.
struct Scale {
let notes = ["C", "D", "E", "F", "G", "A", "B"]
}
struct ScaleIterator: IteratorProtocol {
private let scale: Scale
private var index = 0
init(_ scale: Scale) {
self.scale = scale
}
mutating func next() -> String? {
if index == scale.notes.count {
index = 0
}
defer {
index += 1
}
return scale.notes[index]
}
}
extension Scale: Sequence {
func makeIterator() -> ScaleIterator {
return ScaleIterator(self)
}
}
let scale = Scale()
for note in scale.prefix(20) {
print(note)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment