Last active
March 21, 2017 13:10
-
-
Save desmondmc/a3531e647881cc9b0df1fd30be237ca5 to your computer and use it in GitHub Desktop.
Sequence Extensions
This file contains hidden or 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
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