therealbnut / Either

Either Protocol

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Either

A reusable Swift Either protocol, easy to conform to, extensible, and with useful functions.

Example Usage:

let results: [Result<Int,MyError>] = ...
let values: [Int] = results.filterSecond()

Example Conformance:

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)
        }
    }
}

About

Either Protocol

License:MIT License


Languages

Language:Swift 100.0%