The easiest solution is to use indices rather than enumerate, then use subscript to fetch the value:
func find<C : CollectionType>(domain: C, comparator: (C.Generator.Element) -> Bool) -> C.Index? {
for index in indices(domain) {
if comparator(domain[index]) {
return index
}
}
return nil
}
but I'm guessing you already knew that and wanted a solution that got you both the element and the index together without needing subscript. You could use Zip2 to do this:
func find<C : CollectionType>(domain: C, comparator: (C.Generator.Element) -> Bool) -> C.Index? {
for (index, element) in Zip2(indices(domain), domain) {
if comparator(element) {
return index
}
}
return nil
}
edit: as Rob points out below, this is guaranteed to work per the swift docs.
However this relies on something that makes me slightly uneasy, which is assuming that iterating over a collection via it's generator is guaranteed to return values in the same order as they are indexed. It would possibly be a slightly obnoxious thing for a collection to not do this (and since most collections use IndexingGenerator to serve up their generators, it's very likely to be the case). But I don't think it's documented as a guarantee so could be considered iffy.
enumeratenotgenerate, right?