Skip to content

Instantly share code, notes, and snippets.

@dotWasim
Created April 4, 2020 11:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dotWasim/2d1f94dd9819729f0a31d6c53f9600b5 to your computer and use it in GitHub Desktop.
Save dotWasim/2d1f94dd9819729f0a31d6c53f9600b5 to your computer and use it in GitHub Desktop.
add count(where:) to Sequence protocol
extension Sequence {
func count(where predicate: (Element) throws -> Bool) rethrows -> Int {
try reduce(0) { try predicate($1) ? $0 + 1 : $0 }
}
}
let count = [1,23,4].count(where: { $0 > 2})
print(count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment