A reusable Swift Either protocol, easy to conform to, extensible, and with useful functions.
let results: [Result<Int,MyError>] = ...
let values: [Int] = results.filterSecond()
enum Result<T,E: ErrorType> {
case Value(T), Error(E)
}
extension Result: EitherType {
func apply(
@noescape first first: E throws -> Void,
@noescape second: T throws -> Void
) rethrows {
switch self {
case let .Error(x): try first(x)
case let .Value(x): try second(x)
}
}
}