Should there be BidirectionalCollection.dropLast(while:)?

There is Collection.drop(while:) which drops elemenst from the beginning.

Look like Swift Algorithms added BidirectionalCollection.suffix(while:)

I think for symmetry and completeness, there should be BidirectionalCollection.dropLast(while:)

I needed this so I made one like this:

extension BidirectionalCollection {
    func dropLast(while predicate: (Element) throws -> Bool) rethrows -> Self.SubSequence {
        let suffix = try suffix(while: predicate)
        return dropLast(suffix.count)

is there correct? is there better way?

Avoid calling count on a non-RandomAccess collection, by leveraging the fact that a slice shares indices with its base collection:

return self[..<suffix.startIndex]

Or, using only standard library methods:

let i = try lastIndex{ try !predicate($0) }

guard let j = i else {
  return self[..<startIndex]

return self[...j]

I don't understand why this works to return the whole thing as Subsequence:

return self[..<startIndex]   // why is the range result in the entire thing as Subsequence?

is it because ..<startIndex is an empty range? Then shouldn't it be an empty Subsequence?

these two I can understand:

return self[..<endIndex]
return self[...]     // I prefer this form

Thanks for the answer, I learned something new about when not to use count

It returns an empty subsequence.

1 Like

okay, I understand now. The guard ... else { } is executed when every element in the collection match the predicate, so an empty Subsequence is returned.


1 Like
Terms of Service

Privacy Policy

Cookie Policy