Skip to content

Instantly share code, notes, and snippets.

@ishikawa
Created July 20, 2014 10:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ishikawa/bd4347ebecf0c254debd to your computer and use it in GitHub Desktop.
Save ishikawa/bd4347ebecf0c254debd to your computer and use it in GitHub Desktop.
Swift find function which takes predicate closure
import Foundation
func find<C : Collection>(source: C, includeElement: (C.GeneratorType.Element) -> Bool) -> C.IndexType? {
for i in indices(source) {
if includeElement(source[i]) {
return i;
}
}
return nil
}
@milesegan
Copy link

// Updated for Swift 1.2

func find<C : CollectionType>(source: C, predicate: (C.Generator.Element) -> Bool) -> C.Index? {
    for i in indices(source) {
        if predicate(source[i]) {
            return i
        }
    }

    return nil
}

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