Skip to content

Instantly share code, notes, and snippets.

@brocoo
Created August 19, 2015 15:41
Show Gist options
  • Save brocoo/c294ec12b10d1af524ec to your computer and use it in GitHub Desktop.
Save brocoo/c294ec12b10d1af524ec to your computer and use it in GitHub Desktop.
Add a firstMatching function for CollectionType
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