Swift: stdlib’s `find` should take a predicate
| Originator: | rix.rob | ||
| Number: | rdar://17655838 | Date Originated: | 13-Jul-2014 01:27 PM |
| Status: | Open | Resolved: | |
| Product: | Developer Tools | Product Version: | Xcode6-Beta3 (6A254o) |
| Classification: | Enhancement | Reproducible: | Always |
Summary:
`find` takes a Collection of Equatable Elements and an example Element and returns the index of the first one which is equal to the passed Element. This is valuable as an `indexOf` case, but it would be more valuable for it to take a predicate instead of an element (or for an overload to exist which does this.
For example:
func find<C : Swift.Collection>(domain: C, predicate: C.GeneratorType.Element -> Bool) -> C.IndexType? {
for index in domain.startIndex..<domain.endIndex {
if predicate(domain[index]) { return index }
}
return nil
}
This returns the index of the first member of the collection for whom the predicate returns `true`, or else it returns `nil`.
I can use this `find` to do a linear search through an array of tuples looking for one whose first element is equal to something, whereas the existing one won’t work for two reasons:
1. Tuples aren’t Equatable
2. I want to match only on the tuple’s first element, not the whole tuple
Steps to Reproduce:
N/A
Expected Results:
N/A
Actual Results:
N/A
Regression:
N/A
Notes:
<3
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!