Skip to content

Instantly share code, notes, and snippets.

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 airspeedswift/f413beedc42efe76263466263bf80d24 to your computer and use it in GitHub Desktop.
Save airspeedswift/f413beedc42efe76263466263bf80d24 to your computer and use it in GitHub Desktop.
partial count(where:)
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