mirror of
https://github.com/apple/swift-algorithms
synced 2026-11-26 04:27:48 +00:00
Compare commits
3
Commits
main
...
nate/count_where
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
33b053c945 | ||
|
|
7abf1b689a | ||
|
|
f6daa2b84c |
No files matched your search
@@ -0,0 +1,6 @@
|
|||||||
|
# Count Where
|
||||||
|
|
||||||
|
[[Source](https://github.com/apple/swift-algorithms/blob/main/Sources/Algorithms/Count.swift) |
|
||||||
|
[Tests](https://github.com/apple/swift-algorithms/blob/main/Tests/SwiftAlgorithmsTests/CountTests.swift)]
|
||||||
|
|
||||||
|
TK
|
||||||
@@ -40,6 +40,7 @@ Read more about the package, and the intent behind it, in the [announcement on s
|
|||||||
#### Other useful operations
|
#### Other useful operations
|
||||||
|
|
||||||
- [`chunked(by:)`, `chunked(on:)`, `chunks(ofCount:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Chunked.md): Eager and lazy operations that break a collection into chunks based on either a binary predicate or when the result of a projection changes or chunks of a given count.
|
- [`chunked(by:)`, `chunked(on:)`, `chunks(ofCount:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Chunked.md): Eager and lazy operations that break a collection into chunks based on either a binary predicate or when the result of a projection changes or chunks of a given count.
|
||||||
|
- [`count(where:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Count.md): Counts the elements in a sequence that pass a predicate.
|
||||||
- [`firstNonNil(_:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/FirstNonNil.md): Returns the first non-`nil` result from transforming a sequence's elements.
|
- [`firstNonNil(_:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/FirstNonNil.md): Returns the first non-`nil` result from transforming a sequence's elements.
|
||||||
- [`indexed()`](https://github.com/apple/swift-algorithms/blob/main/Guides/Indexed.md): Iterate over tuples of a collection's indices and elements.
|
- [`indexed()`](https://github.com/apple/swift-algorithms/blob/main/Guides/Indexed.md): Iterate over tuples of a collection's indices and elements.
|
||||||
- [`interspersed(with:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Intersperse.md): Place a value between every two elements of a sequence.
|
- [`interspersed(with:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Intersperse.md): Place a value between every two elements of a sequence.
|
||||||
|
|||||||
@@ -0,0 +1,64 @@
|
|||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
//
|
||||||
|
// 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.countAll(where: { $0.count < 5 })
|
||||||
|
/// // shortNameCount == 3
|
||||||
|
///
|
||||||
|
/// To find the number of times a specific element appears in the sequence,
|
||||||
|
/// use the `countAll(_:)` method.
|
||||||
|
///
|
||||||
|
/// let birds = ["duck", "duck", "duck", "duck", "goose"]
|
||||||
|
/// let duckCount = birds.countAll("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
|
||||||
|
/// `predicate`.
|
||||||
|
@inlinable
|
||||||
|
public func countAll(where predicate: (Element) throws -> Bool) rethrows -> Int {
|
||||||
|
try reduce(0) { try $0 + (predicate($1) ? 1 : 0) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension Sequence where Element: Equatable {
|
||||||
|
/// Returns the number of elements in the sequence that are equal to the given
|
||||||
|
/// value.
|
||||||
|
///
|
||||||
|
/// This example finds the number of times that the string `"duck"` appears
|
||||||
|
/// in an array:
|
||||||
|
///
|
||||||
|
/// let birds = ["duck", "duck", "duck", "duck", "goose"]
|
||||||
|
/// let duckCount = birds.countAll("duck")
|
||||||
|
/// // duckCount == 4
|
||||||
|
///
|
||||||
|
/// The sequence must be finite.
|
||||||
|
///
|
||||||
|
/// - Parameter element: The element to count instances of in this sequence.
|
||||||
|
/// - Returns: The number of elements in the sequence that are equal to
|
||||||
|
/// `element`.
|
||||||
|
@inlinable
|
||||||
|
public func countAll(_ element: Element) -> Int {
|
||||||
|
reduce(0) { $0 + ((element == $1) ? 1 : 0) }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
//
|
||||||
|
// 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 testCountAllWhere() {
|
||||||
|
XCTAssertEqual(0, "".countAll(where: { $0.isNumber }))
|
||||||
|
XCTAssertEqual(0, "Hello".countAll(where: { $0.isNumber }))
|
||||||
|
|
||||||
|
for c in "123-----".uniquePermutations(ofCount: 1...) {
|
||||||
|
let expected = c.lazy.filter(\.isNumber).count
|
||||||
|
XCTAssertEqual(expected, c.countAll(where: \.isNumber))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func testCountAllEquatable() {
|
||||||
|
XCTAssertEqual(0, "".countAll("-"))
|
||||||
|
XCTAssertEqual(0, "Hello".countAll("-"))
|
||||||
|
|
||||||
|
for c in "123-----".uniquePermutations(ofCount: 1...) {
|
||||||
|
let expected = c.lazy.filter { $0 == "-" }.count
|
||||||
|
XCTAssertEqual(expected, c.countAll("-"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user