Compare commits

...
5 changed files with 115 additions and 1 deletions

No files matched your search

+9 -1
View File
@@ -16,6 +16,12 @@ package updates, you can specify your package dependency using
---
## [0.1.1] - 2021-04-14
### Fixes
- `Product2` associated type inference error in release build ([#130])
## [0.1.0] - 2021-04-13
### Additions
@@ -149,7 +155,8 @@ This changelog's format is based on [Keep a Changelog](https://keepachangelog.co
<!-- Link references for releases -->
[Unreleased]: https://github.com/apple/swift-algorithms/compare/0.1.0...HEAD
[Unreleased]: https://github.com/apple/swift-algorithms/compare/0.1.1...HEAD
[0.1.1]: https://github.com/apple/swift-algorithms/compare/0.1.0...0.1.1
[0.1.0]: https://github.com/apple/swift-algorithms/compare/0.0.4...0.1.0
[0.0.4]: https://github.com/apple/swift-algorithms/compare/0.0.3...0.0.4
[0.0.3]: https://github.com/apple/swift-algorithms/compare/0.0.2...0.0.3
@@ -178,6 +185,7 @@ This changelog's format is based on [Keep a Changelog](https://keepachangelog.co
[#112]: https://github.com/apple/swift-algorithms/pull/112
[#124]: https://github.com/apple/swift-algorithms/pull/124
[#125]: https://github.com/apple/swift-algorithms/pull/125
[#130]: https://github.com/apple/swift-algorithms/pull/130
<!-- Link references for contributors -->
+6
View File
@@ -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
+1
View File
@@ -40,6 +40,7 @@ Read more about the package, and the intent behind it, in the [announcement on s
#### 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.
- [`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.
- [`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.
+64
View File
@@ -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("-"))
}
}
}