Skip to content

Instantly share code, notes, and snippets.

@sgbasaraner
Created September 9, 2018 03:53
Show Gist options
  • Save sgbasaraner/5e441d56c0d25c6258b627d4e0b02c8c to your computer and use it in GitHub Desktop.
Save sgbasaraner/5e441d56c0d25c6258b627d4e0b02c8c to your computer and use it in GitHub Desktop.
Rust-like nth() method in Swift
extension Collection {
/// Returns the element at the specified index if it is within bounds, otherwise nil.
func nth(_ index: Index) -> Element? {
return indices.contains(index) ? self[index] : nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment