Skip to content

Instantly share code, notes, and snippets.

@jemmons
Created August 10, 2018 16:46
Show Gist options
  • Save jemmons/0dfb4f4ede3d54ea575e2e5730dfe16b to your computer and use it in GitHub Desktop.
Save jemmons/0dfb4f4ede3d54ea575e2e5730dfe16b to your computer and use it in GitHub Desktop.
public enum Result<Value> {
case success(Value)
case failure(Error)
public init(_ value: Value) {
self = .success(value)
}
public init(_ error: Error) {
self = .failure(error)
}
public func flatMap<U>(transform ƒ: (Value)->Result<U>) -> Result<U> {
switch self {
case .success(let value):
return ƒ(value)
case .failure(let e):
return Result<U>.failure(e)
}
}
public func asyncFlatMap<U>(completion: (Result<U>)->Void, transform ƒ: (Value)->Result<U>) {
completion(flatMap(transform: ƒ))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment