Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
extension Sequence {
func reduce<A>(_ initial: A, combine: (inout A, Iterator.Element) -> ()) -> A {
var result = initial
for element in self {
combine(&result, element)
}
return result
}
}
// Example implementation of `filter` using `reduce`:
extension Sequence {
func myFilter(condition: (Iterator.Element) -> Bool) -> [Iterator.Element] {
return reduce([]) { (result: inout [Iterator.Element], element) in
guard condition(element) else { return }
result.append(element)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.