Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save soltrinox/9d8865497fd0de1739d285b32db14661 to your computer and use it in GitHub Desktop.
Save soltrinox/9d8865497fd0de1739d285b32db14661 to your computer and use it in GitHub Desktop.
Swift circular array subscript
extension Array {
subscript(circular index: Int) -> Element? {
var i = index
if i < 0 || isEmpty {
return nil
} else if i > count - 1 {
i = index % count
}
return self[i]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment