Either
public enum Either<L, R>
The Either
type represents values with two possibilities: a value of type Either<A, B>
is either left(a)
or
right(b)
.
The Either
type is sometimes used to represent a value which is either correct or an error; by convention,
the left
constructor is used to hold an error value and the right
constructor is used to hold a correct
value (mnemonic: right
also means correct
).
Examples:
The type Either<String, Int>
is the type of values which can be either a String
or an Int
. The left
constructor can be used only on String
s, and the right
constructor can be used only on Int
s:
>>> let s: Either<String, Int> = .left("foo")
>>> s
s: Either<String, Int> = left {
left = "foo"
}
>>> let n: Either<String, Int> = .right(3)
>>> n
n: Either<String, Int> = right {
right = 3
}
>>> type(of: s)
Either<String, Int>.Type = Either<String, Int>
>>> type(of: n)
Either<String, Int>.Type = Either<String, Int>
The map(_:)
from our Functor
instance will ignore left
values, but will apply the supplied function to values
contained in a right
:
>>> let s: Either<String, Int> = .left("foo")
>>> let n: Either<String, Int> = .right(3)
>>> s.map { $0 * 2 }
Either<String, Int> = left {
left = "foo"
}
>>> n.map { $0 * 2 }
Either<String, Int> = right {
right = 6
}
-
Left value of
Either
.If
Either
used as result of an operation this contains error.Declaration
Swift
case left(L)
-
Right value of
Either
.If
Either
used as result of an operation this contains success value.Declaration
Swift
case right(R)
-
Evaluates the given closure when this
Either
instance isright
, passing the unwrapped value as a parameter.Use the
map(_:)
method with a closure that returns a non-either value. This example performs an arithmetic operation on an either integer.>>> func stringToInt(_ string: String) -> Either<String, Int> { >>> switch Int(string) { >>> case let .some(int): >>> return .right(int) >>> case .none: >>> return .left("Not an integer") >>> } >>> } >>> let possibleNumber = stringToInt("42") >>> let possibleSquare = possibleNumber.map { $0 * $0 } >>> possibleSquare Either<String, Int> = right { right = 1764 } >>> let noNumber: Int? = stringToInt("test") >>> let noSquare = noNumber.map { $0 * $0 } >>> noSquare Either<String, Int> = left { left = "Not an integer" }
Declaration
Swift
public func map<T>(_ transform: (R) -> T) -> Either<L, T>
Parameters
transform
A closure that takes the unwrapped value of the instance.
-
Evaluates the given closure when this
Either
instance is notleft
, passing the unwrapped value as a parameter.Use the
flatMap(_:)
method with a closure that returns an either value. This example performs an arithmetic operation with an either result on an either integer.>>> let possibleNumber: Either<String, Int> = stringToInt("42") >>> let nonOverflowingSquare = possibleNumber.flatMap { x -> Either<String, Int> in >>> let (result, overflowed) = x.multipliedReportingOverflow(by: x) >>> return overflowed == .overflow ? "Square overflow integer capacity" : result >>> } >>> nonOverflowingSquare Either<String, Int> = right { right = 1764 } >>> let noNumber: Int? = stringToInt("test") >>> let nonOverflowingSquare = possibleNumber.flatMap { x -> Either<String, Int> in >>> let (result, overflowed) = x.multipliedReportingOverflow(by: x) >>> return overflowed == .overflow ? "Square overflow integer capacity" : result >>> } >>> nonOverflowingSquare Either<String, Int> = left { left = "Not an integer" } >>> let possibleNumber: Either<String, Int> = stringToInt("1234567890123") >>> let nonOverflowingSquare = possibleNumber.flatMap { x -> Either<String, Int> in >>> let (result, overflowed) = x.multipliedReportingOverflow(by: x) >>> return overflowed == .overflow ? "Square overflow integer capacity" : result >>> } >>> nonOverflowingSquare Either<String, Int> = left { left = "Square overflow integer capacity" }
Declaration
Swift
public func flatMap<T>(_ transform: (R) -> Either<L, T>) -> Either<L, T>
Parameters
transform
A closure that takes the unwrapped value of the instance.
-
Evaluates the given closure when this
Either
instance isleft
, passing the unwrapped value as a parameter.Use the
second(_:)
method for evaluates when thisEither
isright
.See also
map(_:)
,second(_:)
methods.Declaration
Swift
public func first<T>(_ transform: (L) -> T) -> Either<T, R>
Parameters
transform
A closure that takes the unwrapped value of the instance.
-
Evaluates the given closure when this
Either
instance isright
, passing the unwrapped value as a parameter.This is a synonym for the
map(_:)
method.Use the
first(_:)
method for evaluates when thisEither
isleft
.Declaration
Swift
public func second<T>(_ f: (R) -> T) -> Either<L, T>
Parameters
transform
A closure that takes the unwrapped value of the instance.
-
Evaluates the first given closure when this
Either
instance isleft
, and second given closure when thisEither
isright
Applying this method to Bifunctor is equivalent to applying
first(_:)
andsecond(_:)
methods sequentially.See also
first(_:)
,second(_:)
methods.Declaration
Swift
public func bimap<T, Z>(_ f: (L) -> T, _ s: (R) -> Z) -> Either<T, Z>
Parameters
f
A closure that takes the unwrapped value of the first.
s
A closure that takes the unwrapped value of the second.
Return Value
The result of the given closure.
-
Evaluates the given closure when this
Either
instance isleft
, passing the unwrapped value as a parameter.This is a synonym for the
first(_:)
method.Use the
second(_:)
method for evaluates when thisEither
isright
.See also
first(_:)
method.Declaration
Swift
public func mapLeft<T>(_ transform: (L) -> T) -> Either<T, R>
Parameters
transform
A closure that takes the unwrapped value of the instance.
-
Case analysis for the Either type.
If the value is
left
, apply the first function toleft
; if it isright
, apply the second function toright
.Declaration
Swift
public func either<T>(_ f: (L) -> T, _ s: (R) -> T) -> T
Parameters
f
The first function.
s
The second function.
Return Value
The result of the first or second function.