From f6daa2b84cebc1acd981b2fc1601da9943397940 Mon Sep 17 00:00:00 2001 From: Nate Cook Date: Tue, 20 Apr 2021 11:08:55 -0500 Subject: [PATCH] Bring count(where:) from @khanlou's proposal --- Sources/Algorithms/Count.swift | 42 +++++++++++++++++++++ Tests/SwiftAlgorithmsTests/CountTests.swift | 25 ++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 Sources/Algorithms/Count.swift create mode 100644 Tests/SwiftAlgorithmsTests/CountTests.swift diff --git a/Sources/Algorithms/Count.swift b/Sources/Algorithms/Count.swift new file mode 100644 index 0000000..af753a7 --- /dev/null +++ b/Sources/Algorithms/Count.swift @@ -0,0 +1,42 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Algorithms open source project +// +// Copyright (c) 2021 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// +//===----------------------------------------------------------------------===// + +extension Sequence { + /// Returns the number of elements in the sequence that satisfy the given + /// predicate. + /// + /// You can use this method to count the number of elements that pass a test. + /// For example, this code finds the number of names that are fewer than + /// five characters long: + /// + /// let names = ["Jacqueline", "Ian", "Amy", "Juan", "Soroush", "Tiffany"] + /// let shortNameCount = names.count(where: { $0.count < 5 }) + /// // shortNameCount == 3 + /// + /// To find the number of times a specific element appears in the sequence, + /// use the equal-to operator (`==`) in the closure to test for a match. + /// + /// let birds = ["duck", "duck", "duck", "duck", "goose"] + /// let duckCount = birds.count(where: { $0 == "duck" }) + /// // duckCount == 4 + /// + /// The sequence must be finite. + /// + /// - Parameter predicate: A closure that takes each element of the sequence + /// as its argument and returns a Boolean value indicating whether + /// the element should be included in the count. + /// - Returns: The number of elements in the sequence that satisfy the given + /// predicate. + @inlinable + public func count(where predicate: (Element) throws -> Bool) rethrows -> Int { + try reduce(0) { try $0 + (predicate($1) ? 1 : 0) } + } +} diff --git a/Tests/SwiftAlgorithmsTests/CountTests.swift b/Tests/SwiftAlgorithmsTests/CountTests.swift new file mode 100644 index 0000000..990b418 --- /dev/null +++ b/Tests/SwiftAlgorithmsTests/CountTests.swift @@ -0,0 +1,25 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Algorithms open source project +// +// Copyright (c) 2021 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// +//===----------------------------------------------------------------------===// + +import XCTest +import Algorithms + +final class CountTests: XCTestCase { + func testCountWhere() { + XCTAssertEqual(0, "".count(where: { $0.isNumber })) + XCTAssertEqual(0, "Hello".count(where: { $0.isNumber })) + + for c in "123-----".uniquePermutations(ofCount: 1...) { + let expected = c.lazy.filter(\.isNumber).count + XCTAssertEqual(expected, c.count(where: \.isNumber)) + } + } +}