Created
May 11, 2018 21:11
-
-
Save robtimp/df3bd937f2eed81e6d2cf771980fa397 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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