Created
July 4, 2018 14:01
-
-
Save onmyway133/402950f93fc47bc15e05df5dc07c5ad3 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
enum Result<T> { | |
case value(value: T) | |
case failure(error: Error) | |
// Sync | |
public func map<U>(f: (T) -> U) -> Result<U> { | |
switch self { | |
case let .value(value): | |
return .value(value: f(value)) | |
case let .failure(error): | |
return .failure(error: error) | |
} | |
} | |
// Async | |
public func map<U>(f: @escaping ((T), (U) -> Void) -> Void) -> (((Result<U>) -> Void) -> Void) { | |
return { g in // g: Result<U> -> Void | |
switch self { | |
case let .value(value): | |
f(value) { transformedValue in // transformedValue: U | |
g(.value(value: transformedValue)) | |
} | |
case let .failure(error): | |
g(.failure(error: error)) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment