Skip to content

Instantly share code, notes, and snippets.

@jshier
Created December 8, 2015 19:10
Show Gist options
  • Save jshier/73e184c727b74c8f86f9 to your computer and use it in GitHub Desktop.
Save jshier/73e184c727b74c8f86f9 to your computer and use it in GitHub Desktop.
extension SequenceType {
@warn_unused_result
func firstElement(@noescape predicate: (Self.Generator.Element) throws -> Bool) rethrows -> Self.Generator.Element? {
for element in self {
if try predicate(element) {
return element
}
}
return nil
}
}
@jshier
Copy link
Author

jshier commented Dec 8, 2015

Swift SequenceType extension to get the first element of a collection that matches the predicate closure passed in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment