Either

public final class Either<A, B> : EitherOf<A, B>

Sum type of types A and B. Represents a value of either one of those types, but not both at the same time. Values of type A are called left; values of type B are called right.

  • Constructs a left value.

    Declaration

    Swift

    public static func left(_ a: A) -> Either<A, B>

    Parameters

    a

    Value to be wrapped in a left of this Either type.

    Return Value

    A left value of Either.

  • Constructs a right value

    Declaration

    Swift

    public static func right(_ b: B) -> Either<A, B>

    Parameters

    b

    Value to be wrapped in a right of this Either type.

    Return Value

    A right value of Either.

  • Safe downcast.

    Declaration

    Swift

    public static func fix(_ fa: EitherOf<A, B>) -> Either<A, B>

    Parameters

    fa

    Value in the higher-kind form.

    Return Value

    Value cast to Either.

  • Applies the provided closures based on the content of this Either value.

    Declaration

    Swift

    public func fold<C>(_ fa: (A) -> C, _ fb: (B) -> C) -> C

    Parameters

    fa

    Closure to apply if the contained value in this Either is a member of the left type.

    fb

    Closure to apply if the contained value in this Either is a member of the right type.

    Return Value

    Result of applying the corresponding closure to this value.

  • Checks if this value belongs to the left type.

    Declaration

    Swift

    public var isLeft: Bool { get }
  • Checks if this value belongs to the right type.

    Declaration

    Swift

    public var isRight: Bool { get }
  • Attempts to obtain a value of the left type.

    This propery is unsafe and can cause fatal errors if it is invoked on a right value.

    Declaration

    Swift

    public var leftValue: A { get }
  • Attempts to obtain a value of the right type.

    This property is unsafe and can cause fatal errors if it is invoked on a left value.

    Declaration

    Swift

    public var rightValue: B { get }
  • Returns the value of the right type, or nil if it is a left value.

    Declaration

    Swift

    public var orNil: B? { get }
  • Reverses the types of this either. Left values become right values and vice versa.

    Declaration

    Swift

    public func swap() -> Either<B, A>

    Return Value

    An either value with its types reversed respect to this one.

  • Transforms both type parameters, preserving the structure of this value.

    Declaration

    Swift

    public func bimap<C, D>(_ fa: (A) -> C, _ fb: (B) -> D) -> Either<C, D>

    Parameters

    fa

    Closure to be applied when there is a left value.

    fb

    Closure to be applied when there is a right value.

    Return Value

    Result of applying the corresponding closure to this value.

  • Transforms the left type parameter, preserving the structure of this value.

    Declaration

    Swift

    public func mapLeft<C>(_ f: (A) -> C) -> Either<C, B>

    Parameters

    f

    Transforming closure.

    Return Value

    Result of appliying the transformation to any left value in this value.

  • Returns the value from this Either.right value or allows callers to transform the Either.left to Either.right.

    Declaration

    Swift

    public func getOrHandle(_ f: (A) -> B) -> B

    Parameters

    f

    Left transforming function.

    Return Value

    Value of this Either.right or transformation of this Either.left.

  • Converts this Either to an Option.

    This conversion is lossy. Left values are mapped to Option.none() and right values to Option.some(). The original `Either cannot be reconstructed from the output of this conversion.

    Declaration

    Swift

    public func toOption() -> Option<B>

    Return Value

    An option containing a right value, or none if there is a left value.

  • Obtains the value wrapped if it is a right value, or the default value provided as an argument.

    Declaration

    Swift

    public func getOrElse(_ defaultValue: B) -> B

    Parameters

    defaultValue

    Value to be returned if this value is left.

    Return Value

    The wrapped value if it is right; otherwise, the default value.

  • Filters the right values, providing a default left value if the do not match the provided predicate.

    Declaration

    Swift

    public func filterOrElse(_ predicate : @escaping (B) -> Bool, _ defaultValue : A) -> Either<A, B>

    Parameters

    predicate

    Predicate to test the right value.

    defaultValue

    Value to be returned if the right value does not satisfies the predicate.

    Return Value

    This value, if it matches the predicate or is left; otherwise, a left value wrapping the default value.

  • Filters the right values, providing a function to transform those that do not match the predicate into a left-type value.

    Declaration

    Swift

    public func filterOrOther(_ predicate: @escaping (B) -> Bool, _ f: @escaping (B) -> A) -> Either<A, B>

    Parameters

    predicate

    Filtering predicate.

    f

    Transforming function.

    Return Value

    This value, if it matches the predicate or is left; otherwise, a left value wrapping the transformation of the right value.

  • Flattens the right side of this value, providing a default value in case the wrapped value is not present.

    Declaration

    Swift

    public func leftIfNull<BB>(_ f: @escaping @autoclosure () -> A) -> Either<A, BB> where B == Optional<BB>

    Parameters

    f

    Function providing a default value.

    Return Value

    An Either value where the right side is not optional.

  • Provides an Iso to go from/to this type to its Kind version.

    Declaration

    Swift

    static var fixIso: Iso<Either<A, B>, EitherOf<A, B>> { get }
  • Provides a Fold based on the Foldable instance of this type.

    Declaration

    Swift

    static var fold: Fold<Either<A, B>, B> { get }
  • Provides a Traversal based on the Traverse instance of this type.

    Declaration

    Swift

    static var traversal: Traversal<Either<A, B>, B> { get }
  • Undocumented

    Declaration

    Swift

    public typealias EachFoci = B
  • Declaration

    Swift

    public static var each: Traversal<Either<A, B>, B> { get }
  • Provides a polymorphic Iso between Either and Validated.

    Declaration

    Swift

    static func toPValidated<C, D>() -> PIso<Either<A, B>, Either<C, D>, Validated<A, B>, Validated<C, D>>

    Return Value

    A polymorphic Iso between Either and Validated.

  • Provides an Iso between Either and Validated.

    Declaration

    Swift

    static var toValidated: Iso<Either<A, B>, Validated<A, B>> { get }
  • Provides a Prism focused on the left side of an Either.

    Declaration

    Swift

    static var leftPrism: Prism<Either<A, B>, A> { get }
  • Provides a Prism focused on the right side of an Either.

    Declaration

    Swift

    static var rightPrism: Prism<Either<A, B>, B> { get }
  • Checks if this value has an element in the right side.

    Declaration

    Swift

    func contains(_ element: B) -> Bool

    Parameters

    element

    Element to check.

    Return Value

    Boolean value indicating if the element was found or not.

  • Declaration

    Swift

    public var description: String { get }
  • Declaration

    Swift

    public func combine(_ other: Either<A, B>) -> Either<A, B>
  • Declaration

    Swift

    public static func empty() -> Either<A, B>
  • Converts this Either into a Result value.

    Declaration

    Swift

    func toResult() -> Result<B, A>

    Return Value

    A Result.success if this is an Either.right, or a Result.failure if this is an Either.left.