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

Proposal: Add last(where:) and lastIndex(where:) Methods to Collections #329

Closed
wants to merge 6 commits into from

Conversation

natecook1000
Copy link
Member

@natecook1000 natecook1000 commented May 17, 2016

The standard library should include methods for finding the last element of a collection that matches a predicate, along with the index of that element.

→ View Proposal

@natecook1000 natecook1000 changed the title Proposal: Add last(where:) and lastIndex(where:) Methods to Bidirectional Collections Proposal: Add last(where:) and lastIndex(where:) Methods to Collections May 24, 2016

Unfortunately, there are no such methods that search from the end. Finding the last of a particular kind of element has multiple applications, particularly with text, such as wrapping a long string into lines of a maximum length or trimming whitespace from the beginning and end of a string.

You can work around this limitation by using the methods above on a reversed view of a collection, but the resulting code is truly dreadful. For example, to find the corresponding last index to `a.index(where: { $0 > 25 })`, something lik this unholy incantation is required:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small typo here on "something like this"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@DougGregor
Copy link
Member

I'm closing out PRs for proposals that are out of scope for Swift 4 stage 1. When we open up the process to the point where this proposal can be in scope, please re-open the pull request.

@DougGregor DougGregor closed this Jan 26, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants