Skip to content

Instantly share code, notes, and snippets.

@daehn
Last active January 30, 2016 19:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daehn/402940f8e3470a22e38b to your computer and use it in GitHub Desktop.
Save daehn/402940f8e3470a22e38b to your computer and use it in GitHub Desktop.
Extension on SequenceType to count the occurrence of an element.
extension SequenceType where Generator.Element : Equatable {
func count(other: Generator.Element) -> Int {
var sum = 0
for case other in self { sum++ }
return sum
}
}
func ~=<T>(pattern: T -> Bool, value: T) -> Bool {
return pattern(value)
}
extension SequenceType where Generator.Element : Equatable {
func count(predicate: Generator.Element -> Bool) -> Int {
var sum = 0
for case predicate in self { sum++ }
return sum
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment