Created
August 19, 2015 15:41
-
-
Save brocoo/c294ec12b10d1af524ec to your computer and use it in GitHub Desktop.
Add a firstMatching function for CollectionType
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 CollectionType { | |
/// Returns the first element where `predicate` returns `true` for the | |
/// corresponding value, or `nil` if such value is not found. | |
/// | |
/// - Complexity: O(`self.count`). | |
func firstMatching(@noescape predicate: (Self.Generator.Element) -> Bool) -> Self.Generator.Element? { | |
for (_, element) in self.enumerate() { | |
if predicate(element) { return element } | |
} | |
return nil | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment