Skip to content

Instantly share code, notes, and snippets.

@oisdk
Created September 3, 2015 17:26
Show Gist options
  • Save oisdk/f157b871191b0b9010c5 to your computer and use it in GitHub Desktop.
Save oisdk/f157b871191b0b9010c5 to your computer and use it in GitHub Desktop.
let notes = ["A", "A♯", "B", "C", "C♯", "D", "D♯", "E", "F", "F♯", "G", "G♯"]
extension Strideable {
func strides<S : SequenceType where S.Generator.Element == Stride>(ss: S) -> [Self] {
var i = self
return ss.map { s in
defer { i = i.advancedBy(s) }
return i
}
}
}
extension CollectionType where Index : IntegerArithmeticType {
subscript(mod i: Index) -> Generator.Element {
return self[i % endIndex]
}
}
extension CollectionType where Index : IntegerArithmeticType, Index : Strideable {
func rotateIntervals<S : SequenceType where S.Generator.Element == Index.Stride>(from: Index, by: S) -> [Generator.Element] {
return from.strides(by).map { i in self[mod: i] }
}
}
let major = [2, 2, 1, 2, 2, 2, 1]
notes.rotateIntervals(0, by: major) // ["A", "B", "C♯", "D", "E", "F♯", "G♯"]
notes.rotateIntervals(3, by: major) // ["C", "D", "E", "F", "G", "A", "B"]
let minor = [2, 1, 2, 2, 2, 2, 2]
notes.rotateIntervals(0, by: minor) // ["A", "B", "C", "D", "E", "F♯", "G♯"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment