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 · 6 comments
Closed

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

ddunbar opened this issue May 13, 2016 · 6 comments
Assignees
Labels
feature A feature request or implementation good first issue Good for newcomers standard library Area: Standard library umbrella swift evolution implemented Flag → feature: A feature that was approved through the Swift evolution process and implemented

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

This would need to go through the Swift Evolution Process.

@natecook1000
Copy link
Member

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
@AnthonyLatsis
Copy link
Collaborator

Proposal: SE-0204

@AnthonyLatsis AnthonyLatsis added API request swift evolution implemented Flag → feature: A feature that was approved through the Swift evolution process and implemented good first issue Good for newcomers and removed new feature labels Nov 12, 2022
@AnthonyLatsis AnthonyLatsis added feature A feature request or implementation and removed API request labels Jan 27, 2023
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature A feature request or implementation good first issue Good for newcomers standard library Area: Standard library umbrella swift evolution implemented Flag → feature: A feature that was approved through the Swift evolution process and implemented
Projects
None yet
Development

No branches or pull requests

5 participants