Created
September 9, 2018 03:53
-
-
Save sgbasaraner/5e441d56c0d25c6258b627d4e0b02c8c to your computer and use it in GitHub Desktop.
Rust-like nth() method in Swift
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
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