Created
August 10, 2018 16:46
-
-
Save jemmons/0dfb4f4ede3d54ea575e2e5730dfe16b to your computer and use it in GitHub Desktop.
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 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