Skip to content

Instantly share code, notes, and snippets.

@sam-w
Created November 30, 2015 00:02
Show Gist options
  • Save sam-w/4673e6a67b16769ab05a to your computer and use it in GitHub Desktop.
Save sam-w/4673e6a67b16769ab05a to your computer and use it in GitHub Desktop.
Reducing over ResultType<Value: CollectionType, Error>
public extension ResultType where Value: CollectionType {
public func reduce<U>(initial: U, @noescape combine: (U, Value.Generator.Element) -> Result<U, Error>) -> Result<U, Error> {
return analysis(
ifSuccess: {
$0.reduce(.Success(initial)) { (acc, item) in
return acc.flatMap {
combine($0, item)
}
}
},
ifFailure: Result<U, Error>.Failure)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment