okay, I understand now. The guard ... else { } is executed when every element in the collection match the predicate, so an empty Subsequence is returned.
should endIndex be startIndex to return an empty collection? I think when lastIndex(where:) is nil, you are returning the whole collection. It should be empty.
I think there is an off-by-one error: when lastIndex(where:) found an index, that index position need to be included in the SubSequence. This is what I did:
The Algorithms package does include this method, with the name trimmingSuffix(while:) (implemented here). The package includes a variety of trimming methods, including trimmingPrefix(while:) — a method that shares the functionality of drop(while:) but is more predictably readable at the call site when written with a trailing closure.
Related to the implementation conversation in this thread, we discovered that there are a couple common operations that are used in implementing this kind of functionality: endOfPrefix(while:) and startOfSuffix(while:) (implemented here). startOfSuffix(while:) handles that off-by-one case without recalculating the trailing index.
That would be great! @young, feel free to start a thread in the Swift Algorithms discussion, file an issue, or open a PR to make the change. Happy to discuss in any of those places.
At the C++Now conference 2 weeks ago, I attended a talk I thought I was going to hate: “Why iterators got it all wrong and what we should use instead” by Arno Schoedl. Turns out it was pretty brilliant in fact, and I think it helps explain how I posted several wrong answers in this thread: a fuzziness in the role of indices makes them error-prone. I don't love some details of the library design he proposes (and I think he's gone on to revise those details out since the conference), but his fundamental insight, that sometimes an index represents the position of an element and other times it represents the boundary between elements, is extremely powerful. If Swift ever gets around to revising its collection model, these insights should be taken into consideration.
(YouTube currently shows it as "unlisted"; I got the link from JetBrains's conference page, where some videos have been pre-published. I believe the YouTube link won't change once it goes fully public.)