Skip to content

Instantly share code, notes, and snippets.

@desmondmc
Last active March 21, 2017 13:10
Show Gist options
  • Save desmondmc/a3531e647881cc9b0df1fd30be237ca5 to your computer and use it in GitHub Desktop.
Save desmondmc/a3531e647881cc9b0df1fd30be237ca5 to your computer and use it in GitHub Desktop.
Sequence Extensions
public extension Sequence {
func scan<T>(_ seed: T, function:(T, Iterator.Element) -> T) -> T {
var current: T = seed
for element in self {
current = function(current, element)
}
return current
}
}
extension Sequence {
func count(_ shouldCount: (Iterator.Element) -> Bool) -> Int {
var count: Int = 0
for element in self {
if shouldCount(element) {
count += 1
}
}
return count
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment