Skip to content

Instantly share code, notes, and snippets.

@beccadax
Created July 11, 2014 00:56
Show Gist options
  • Save beccadax/e8c8a549986cfc698c19 to your computer and use it in GitHub Desktop.
Save beccadax/e8c8a549986cfc698c19 to your computer and use it in GitHub Desktop.
Broken findLast() function
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!
}
@beccadax
Copy link
Author

Update: Turns out that enumerate() numbers the elements with Ints, not the indices that actually appear in the collection.

That's a useful behavior.

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