Created
March 5, 2019 01:56
-
-
Save airspeedswift/f413beedc42efe76263466263bf80d24 to your computer and use it in GitHub Desktop.
partial count(where:)
This file contains hidden or 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 p: (Element)->Bool)->Int { | |
return reduce(0) { p($1) ? $0 + 1 : $0 } | |
} | |
} | |
let fn: ((Int) -> Bool) -> Int = [1,2,3].count | |
let n = fn { $0<2 } | |
print(n) // 1 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment