Skip to content

Instantly share code, notes, and snippets.

@onmyway133
Created July 4, 2018 14:01
Show Gist options
  • Save onmyway133/402950f93fc47bc15e05df5dc07c5ad3 to your computer and use it in GitHub Desktop.
Save onmyway133/402950f93fc47bc15e05df5dc07c5ad3 to your computer and use it in GitHub Desktop.
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