Rename count(while:) to countPrefix(while:)

This commit is contained in:
1024jp 2017-03-29 15:12:18 +09:00
parent b2926bc742
commit c01693bc29
5 changed files with 33 additions and 9 deletions

View File

@ -27,7 +27,6 @@
import Foundation
// element
extension Array where Element: Equatable {
/// Remove first collection element that is equal to the given `element`
@ -70,15 +69,33 @@ extension Sequence {
// MARK: - Count
extension Sequence {
/// Count up elements that 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.
func count(_ predicate: (Iterator.Element) -> Bool) -> Int {
var count = 0
for element in self where predicate(element) {
count += 1
}
return count
}
/// Count up elements by enumerating collection until a element shows up 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 count(while predicate: (Iterator.Element) -> Bool) -> Int {
func countPrefix(while predicate: (Iterator.Element) -> Bool) -> Int {
var count = 0
for element in self {
@ -86,7 +103,6 @@ extension Sequence {
count += 1
}
return count
}
@ -94,7 +110,8 @@ extension Sequence {
// IndexSet
// MARK: - IndexSet
extension Array {
/// Remove elements with IndexSet
@ -111,6 +128,7 @@ extension Array {
return indexes.flatMap { index in
guard index < self.count else { return nil }
return self[index]
}
}

View File

@ -111,7 +111,7 @@ extension String {
let startIndex = self.index(index, offsetBy: -MaxEscapesCheckLength, limitedBy: self.startIndex) ?? self.startIndex
let seekCharacters = self.characters[startIndex..<index]
let numberOfEscapes = seekCharacters.reversed().count(while: { $0 == "\\" })
let numberOfEscapes = seekCharacters.reversed().countPrefix { $0 == "\\" }
return (numberOfEscapes % 2 == 1)
}

View File

@ -44,7 +44,7 @@ extension URL {
let pathComponents = self.pathComponents
let basePathComponents = baseURL.pathComponents
let sameCount = zip(basePathComponents, pathComponents).count(while: { $0 == $1 })
let sameCount = zip(basePathComponents, pathComponents).countPrefix { $0 == $1 }
let parentCount = basePathComponents.count - sameCount - 1
let sameComponents = [String](repeating: "..", count: parentCount)
let diffComponents = pathComponents[sameCount..<pathComponents.count]

View File

@ -1,6 +1,7 @@
/*
CollectionTests.swift
Tests
CotEditor
https://coteditor.com
@ -32,9 +33,13 @@ class CollectionTests: XCTestCase {
func testCount() {
XCTAssertEqual([1, 2, 0, -1, 3].count(while: { $0 > 0 }), 2)
XCTAssertEqual([0, 1, 2, 0, -1].count(while: { $0 > 0 }), 0)
XCTAssertEqual([1, 2, 3, 4, 5].count(while: { $0 > 0 }), 5)
XCTAssertEqual([1, 2, 0, -1, 3].count({ $0 > 0 }), 3)
XCTAssertEqual([0, 1, 2, 0, -1].count({ $0 > 0 }), 2)
XCTAssertEqual([1, 2, 3, 4, 5].count({ $0 > 0 }), 5)
XCTAssertEqual([1, 2, 0, -1, 3].countPrefix(while: { $0 > 0 }), 2)
XCTAssertEqual([0, 1, 2, 0, -1].countPrefix(while: { $0 > 0 }), 0)
XCTAssertEqual([1, 2, 3, 4, 5].countPrefix(while: { $0 > 0 }), 5)
}
}

View File

@ -1,6 +1,7 @@
/*
StringCollectionTests.swift
Tests
CotEditor
https://coteditor.com