Closed
Description
We currently provide just one way to trim a collection:
extension BidirectionalCollection {
public func trimming(
while predicate: (Element) throws -> Bool
) rethrows -> SubSequence {
let start = try endOfPrefix(while: predicate)
let end = try self[start...].startOfSuffix(while: predicate)
return self[start..<end]
}
}
We should also provide ways to trim only the start of the collection (trimmingPrefix(while:)
, available to all collections), or only the end (trimmingSuffix(while:)
).
Furthermore, collections for which Self == SubSequence
should have mutating
variants using trim
as the base name. As an example, Collection
's popFirst()
from the standard library is implemented using the same constraint.
And finally, despite String
not being its own SubSequence
, String
should also have access to these mutating variants for the sake of convenience.