Skip to content

Commit

Permalink
Remove .countPrefix(while:) extension
Browse files Browse the repository at this point in the history
  • Loading branch information
1024jp committed Jul 13, 2024
1 parent 44565f3 commit a2962e9
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 27 deletions.
18 changes: 0 additions & 18 deletions CotEditor/Sources/Collection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -185,21 +185,3 @@ extension MutableCollection where Self: RandomAccessCollection {
self.sort { $0[keyPath: keyPath] < $1[keyPath: keyPath] }
}
}



// MARK: - Count

extension Sequence {

/// Counts up elements by enumerating collection until encountering the element that doesn't satisfy the given predicate.
///
/// - Parameters:
/// - predicate: A closure that takes an element of the sequence as its argument
/// and returns a Boolean value indicating whether the element should be counted.
/// - Returns: The number of elements that satisfies the given predicate and are sequentially from the first index.
func countPrefix(while predicate: (Element) throws -> Bool) rethrows -> Int {

try self.prefix(while: predicate).count
}
}
2 changes: 1 addition & 1 deletion CotEditor/Sources/URL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ extension URL {
let pathComponents = self.pathComponents.dropLast()
let basePathComponents = baseURL.pathComponents.dropLast(baseURL.hasDirectoryPath ? 0 : 1)

let sameCount = zip(basePathComponents, pathComponents).countPrefix { $0.0 == $0.1 }
let sameCount = zip(basePathComponents, pathComponents).prefix(while: { $0.0 == $0.1 }).count
let parentCount = basePathComponents.count - sameCount
let parentComponents = [String](repeating: "..", count: parentCount)
let diffComponents = pathComponents[sameCount...]
Expand Down
8 changes: 0 additions & 8 deletions Tests/CollectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,6 @@ struct CollectionTests {
}


@Test func countPrefix() {

#expect([1, 2, 0, -1, 3].countPrefix(while: { $0 > 0 }) == 2)
#expect([0, 1, 2, 0, -1].countPrefix(while: { $0 > 0 }) == 0)
#expect([1, 2, 3, 4, 5].countPrefix(while: { $0 > 0 }) == 5)
}


@Test func mapKeys() {

let dict = [1: 1, 2: 2, 3: 3]
Expand Down

0 comments on commit a2962e9

Please sign in to comment.