I often need to drop elements from the end of a sequence based on a condition (for example, trimming trailing whitespace or filtering out values). Currently Swift only offers drop(while:) to drop from the start of a collection, but no built-in symmetric method to drop from the end. Without it, developers resort to awkward workarounds, like reversing the collection or manual loops. For example:
let arr = [1, 2, 3, 4, 5]
let trimmed = Array(arr.reversed().drop(while: { $0 > 3 })).reversed()
// trimmed == [1, 2, 3]
This is clearly more verbose and harder to read than a hypothetical dropLast(while:). A developer on StackOverflow even asked “I don’t know why they have drop(while:) and did not implement dropLast(while:),” and provided a working extension to illustrate the idea. Swift Algorithms also offers a similar function named trimmingSuffix(while:), which removes trailing elements matching a predicate.
Adding dropLast(while:) would simply provide symmetry with drop(while:). Dave Abrahams (one of the Swift Core Team) noted on the Swift forums that this makes sense for “symmetry and completeness,” and even gave a concise implementation using the existing suffix(while:) method:
extension BidirectionalCollection {
func dropLast(while predicate: (Element) throws -> Bool) rethrows -> SubSequence {
let suffix = try suffix(while: predicate)
return self[..<suffix.startIndex]
}
}
That implementation effectively finds the longest trailing subsequence matching the predicate and drops it. (In fact, Swift Algorithms implements the same logic in its trimmingSuffix(while:) function.)
Why this could help: It makes code more direct and readable. For instance, after this addition one could write:
let trimmed = arr.dropLast(while: { $0 > 3 })
// trimmed == [1, 2, 3]
instead of the reverse/drop/reverse trick. Several forum posts confirm that many Swift users find the absence of dropLast(while:) awkward. The symmetry is intuitive: just as drop(while:) drops elements from the front until the predicate fails, dropLast(while:) would drop from the back. As Nate Cook pointed out, the Algorithms package already has this method (calling it trimmingSuffix(while:)), suggesting it’s a commonly needed operation.
Questions :
- Would
dropLast(while:)fit naturally into the standard library? Are there any naming conflicts or confusion with the existingdropLast(_:)(which drops a fixed number of elements)? The closure-based API is similar todrop(while:), so it seems consistent, but feedback on naming (e.g. preferring a name liketrimmingSuffix(while:)) is welcome. - Are there any performance or implementation pitfalls? For a non-
RandomAccesscollection (likeString), this will necessarily iterate from the end, but that’s no different from usingsuffix(while:). One subtlety: the code must handle the case where every element satisfies the predicate (yielding an empty result) – but as shown above, that’s easily done by returningself[..<startIndex]. - Would this method logically belong on
BidirectionalCollection(so we can move backwards) rather than on allCollections orSequences? (It seemsBidirectionalCollectionis the right minimum requirement.) - Open for discussion: Is there any reason to avoid this addition? For example, are we concerned about API bloat versus utility? Are there existing Swift Evolution threads proposing something similar or any objections known (I didn’t find any formal proposal; only forum comments). Are there alternative designs (e.g. making only the Swift Algorithms version public, or introducing both
trimmingSuffixandtrimmingPrefix) that we should consider?
I’m curious to hear if anyone has ideas about edge cases or alternative approaches. Would dropLast(while:) make your code clearer, or do you see situations where it might cause confusion? Any feedback or suggestions are welcome ツ