Created
April 4, 2020 11:16
-
-
Save dotWasim/2d1f94dd9819729f0a31d6c53f9600b5 to your computer and use it in GitHub Desktop.
add count(where:) to Sequence protocol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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