Last active
January 30, 2016 19:50
-
-
Save daehn/402940f8e3470a22e38b to your computer and use it in GitHub Desktop.
Extension on SequenceType to count the occurrence of an element.
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 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