Skip to content

Instantly share code, notes, and snippets.

@ketzusaka
Created July 20, 2015 19:20
Show Gist options
  • Select an option

  • Save ketzusaka/307c073449c506b340d3 to your computer and use it in GitHub Desktop.

Select an option

Save ketzusaka/307c073449c506b340d3 to your computer and use it in GitHub Desktop.
First in Swift
/**
Find the first element in a sequence passing a test.
:param: source Source sequence to iterate
:param: includeElement Closure to evaluate elements with
:return: First element in the sequence for which includeElement returns true, or nil if none found
*/
public func first<S: SequenceType>(source: S, includeElement: (S.Generator.Element) -> Bool) -> S.Generator.Element? {
var filteredSource = lazy(source).filter(includeElement).generate()
return filteredSource.next()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment