Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reimplemented count(where:) #318

Merged
merged 1 commit into from
May 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions Sources/Core/Extensions/Foundation/Collection+PovioKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,18 @@ public extension Collection where Indices.Iterator.Element == Index {
}

public extension Collection {
/// Conditional element count - https://github.com/apple/swift-evolution/blob/master/proposals/0220-count-where.md
func count(where clause: (Element) -> Bool) -> Int {
lazy.filter(clause).count
/// Conditional element count - https://forums.swift.org/t/refresh-review-se-0220-count-where/66235/4
@inlinable
func count(
where predicate: (Element) throws -> Bool
) rethrows -> Int {
try reduce(0) { n, element in
if try predicate(element) {
n + 1
} else {
n
}
}
}
}

Expand Down