Skip to content

Add trimmingPrefix(while:), trimmingSuffix(while:), and mutating variants #101

Closed
@timvermeulen

Description

@timvermeulen

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.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions