Created
July 11, 2014 00:56
-
-
Save beccadax/e8c8a549986cfc698c19 to your computer and use it in GitHub Desktop.
Broken findLast() function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func findLast<C : Swift.Collection where C.GeneratorType.Element : Equatable>(domain: C, value: C.GeneratorType.Element) -> C.IndexType? { | |
return reduce(enumerate(domain), nil) { (prevIndex: C.IndexType?, pair: (index: C.IndexType, elem: C.GeneratorType.Element)) -> C.IndexType? in | |
if pair.elem == value { | |
return pair.index | |
} | |
else { | |
return prevIndex | |
} | |
} | |
// It doesn't like the combine (closure) parameter: | |
// 'Int' is not a subtype of 'C.IndexType' | |
// But this function never mentions Int! | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Update: Turns out that enumerate() numbers the elements with Ints, not the indices that actually appear in the collection.
That's a useful behavior.