-
-
Save soltrinox/9d8865497fd0de1739d285b32db14661 to your computer and use it in GitHub Desktop.
Swift circular array subscript
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
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