Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-1504] RFE: index(of:) but starting from end #44113

Closed
ddunbar opened this issue May 13, 2016 · 5 comments
Closed

[SR-1504] RFE: index(of:) but starting from end #44113

ddunbar opened this issue May 13, 2016 · 5 comments

Comments

@ddunbar
Copy link
Member

ddunbar commented May 13, 2016

Previous ID SR-1504
Radar None
Original Reporter @ddunbar
Type New Feature
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels New Feature
Assignee @natecook1000
Priority Medium

md5: 2e4233e0695a5d1edce92ed2ae1572d9

Issue Description:

It is sometimes desirable to get the forward index of an element but searching from the end of a bidirectionally iterable collection. For example, consider finding the string suffix following the last period.

This is similar to C++'s `find_last_of` or Python's `rindex`.

@ddunbar
Copy link
Member Author

ddunbar commented May 13, 2016

Possible naming suggestion: lastIndex(of: )

@ddunbar
Copy link
Member Author

ddunbar commented May 13, 2016

Possible implementation:

extension BidirectionalCollection where Iterator.Element : Equatable {
    // RFE: https://bugs.swift.org/browse/SR-1504
    public func lastIndex(of element: Iterator.Element) -> Index? {
        var i = self.endIndex
        while i != self.startIndex {
            self.formIndex(before: &i)
            if self[i] == element {
                return i
            }
        }
        return nil
    }
}

@belkadan
Copy link
Contributor

belkadan commented May 13, 2016

This would need to go through the Swift Evolution Process.

@natecook1000
Copy link
Member

natecook1000 commented Jun 11, 2018

This is implemented as of Swift 4.2.

@mattneub
Copy link

mattneub commented Aug 6, 2018

@natecook1000 The docs now say there is a `firstIndex` and a `lastIndex` but no `index`. Yet there is no compile error when using `index`. Shouldn't this be a warning at least? We should all be migrating to `firstIndex` but nothing calls out the change. I've been using Swift 4.2 since WWDC and I only just noticed this by accident.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants