Skip to content

Instantly share code, notes, and snippets.

@philsquared
Last active August 25, 2017 04:04
Show Gist options
  • Save philsquared/63a0cc5817a6211fe91a12d9f76bd2ad to your computer and use it in GitHub Desktop.
Save philsquared/63a0cc5817a6211fe91a12d9f76bd2ad to your computer and use it in GitHub Desktop.
countIf() in Swift
extension Sequence {
func countIf( predicate: (Self.Iterator.Element) -> Bool ) -> Int {
return reduce( 0 ) { predicate($1) ? $0+1 : $0 }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment