Represents a value of one of two possible types (a disjoint union.)
Instances of Either are either an instance of Left or Right.
Swift Either type like scala.util.Either
++ some in / - scalaz./
Take Right Projection is decided it would be want.
- Swift 2 or later
- iOS 8.0 or later
struct Error {
var reason: String
init(_ reason: String) { self.reason = reason }
}
let resultL = Either<Error, String>.left(Error("failed"))
let resultR = Either<Error, String>.right("ok")
resultR.isRight // true
resultR.isLeft // false
~Either<String, String>.left("failed") ?? "fallback" // failed
// Alias for Right Projection.getOrElse
resultL ?? "fallback" // fallback
resultR ?? "fallback" // success
// Right Projection
resultL ||| "ok" // .Right("ok")
Either<Error, String>.left(Error("failed1")) ||| resultL ||| resultR // .Right("ok")
// Alias for Right Projection.map
resultR.map { "\($0)!" } // .Right("ok!")
resultL.map { "\($0)!" } // .Left(Error("failed"))
func isFull<T>(string: String) -> Either<T, Bool> {
return .right(!string.isEmpty)
}
(resultL >>- isFull).fold(
{ e in e.reason},
{ s in s.description }
)
// failed
(resultR >>- isFull).fold(
{ e in e.reason},
{ s in s.description }
)
// true
- Instance Methods
fold<X>(fa: A -> X, _ fb: B -> X) -> X
swap() -> Either<B, A>
getOrElse(or: () -> B) -> B
Right ProjectionorElse(or: () -> B) -> Either<A, B>
Right ProjectionorElse(or: () -> Either<A, B>) -> Either<A, B>
Right Projectionmap<X>(f: B -> X) -> Either<A, X>
Right ProjectionflatMap<X>(f: B -> Either<A, X>) -> Either<A, X>
Right Projection
- Class Methods
left(value: A) -> Either<A, B>
right(value: B) -> Either<A, B>
cond<A, B>(test: Bool, right: () -> B, left: () -> A) -> Either<A, B>
- Instance Methods
getOrElse(or: () -> A) -> A
foreach<U>(f: A -> U)
forall(f: A -> Bool) -> Bool
exists(f: A -> Bool) -> Bool
map<X>(f: A -> X) -> Either<X, B>
flatMap<X>(f: A -> Either<X, B>) -> Either<X, B>
filter(p: A -> Bool) -> Either<A, B>?
toOption() -> A?
- Instance Methods
getOrElse(or: () -> B) -> B
foreach<U>(f: B -> U)
forall(f: B -> Bool) -> Bool
exists(f: B -> Bool) -> Bool
map<X>(f: B -> X) -> Either<A, X>
flatMap<X>(f: B -> Either<A, X>) -> Either<A, X>
filter(p: B -> Bool) -> Either<A, B>?
toOption() -> B?
Add the following to your Cartfile:
github "to4iki/EitherSwift"
Run carthage update
and follow the steps as described in Carthage's README.
Version 0.36 or higher is required. Add the following to your Podfile:
pod 'EitherSwift'
Make sure that you are integrating your dependencies using frameworks: add use_frameworks!
to your Podfile. Then run pod install
.