Result

@frozen
enum Result<Success, Failure> where Failure : Error
  • Provides a prism focused on the success side of a Result.

    Declaration

    Swift

    static var successPrism: Prism<Result<Success, Failure>, Success> { get }
  • Provides a prism focused on the failure side of a Result.

    Declaration

    Swift

    static var failurePrism: Prism<Result<Success, Failure>, Failure> { get }
  • Provides an Iso between Result and Either.

    Declaration

    Swift

    static var toEither: Iso<Result<Success, Failure>, Either<Failure, Success>> { get }
  • Provides an Iso between Result and Validated.

    Declaration

    Swift

    static var toValidated: Iso<Result<Success, Failure>, Validated<Failure, Success>> { get }
  • Converts this Result into an Either value.

    Declaration

    Swift

    func toEither() -> Either<Failure, Success>

    Return Value

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

  • Converts this Result into a Try value.

    Declaration

    Swift

    func toTry() -> Try<Success>

    Return Value

    A Try.failure if this is a Result.failure, or a Try.success if this is a Result.success

  • Converts this Result into a Validated value.

    Declaration

    Swift

    func toValidated() -> Validated<Failure, Success>

    Return Value

    A Validated.invalid if this is a Result.failure, or a Validated.valid if this is a Result.success.

  • Converts this Result into a ValidatedNEA value.

    Declaration

    Swift

    func toValidatedNEA() -> ValidatedNEA<Failure, Success>

    Return Value

    A Validated.invalid with a NonEmptyArray of the Failure type if this is a Result.failure, or a Validated.valid if this is a Result.success.

  • Converts this Result into an Option value.

    Declaration

    Swift

    func toOption() -> Option<Success>

    Return Value

    Option.none if this is a Result.failure, or Option.some if this is a Result.success.

  • Applies the corresponding closure based on the value contained in this result.

    Declaration

    Swift

    func fold<B>(_ ifFailure: @escaping (Failure) -> B,
                 _ ifSuccess: @escaping (Success) -> B) -> B

    Parameters

    ifFailure

    Closure to be applied if this is a Result.failure.

    ifSuccess

    Closure to be applied if this is a Result.success.

    Return Value

    Output of the execution of the corresponding closure, based on the internal value of this Result.

  • Given a function, provides a new function lifted to the context of this type.

    Declaration

    Swift

    static func lift<B>(_ f: @escaping (Success) -> B) -> (Result<Success, Failure>) -> Result<B, Failure>

    Parameters

    f

    Function to be lifted.

    Return Value

    Function in the context of this type.

  • Transforms the value type with a constant value.

    Declaration

    Swift

    func `as`<B>(_ b: B) -> Result<B, Failure>

    Parameters

    b

    Constant value to replace the value type.

    Return Value

    A new value with the structure of the original value, with its value type transformed.

  • Replaces the value type by the Void type.

    Declaration

    Swift

    func void() -> Result<Void, Failure>

    Return Value

    New value in this context, with Void as value type, preserving the original structure.

  • Transforms the value type and pairs it with its original value.

    Declaration

    Swift

    func product<B>(_ f: @escaping (Success) -> B) -> Result<(Success, B), Failure>

    Parameters

    f

    Transforming function.

    Return Value

    A pair with the original value and its transformation, with the structure of the original value.

  • Transforms the value type by making a tuple with a new constant value to the left of the original value type.

    Declaration

    Swift

    func tupleLeft<B>(_ b: B) -> Result<(B, Success), Failure>

    Parameters

    b

    Constant value for the tuple.

    Return Value

    A new value with the structure of the original value, with a tuple in its value type.

  • Transforms the value type by making a tuple with a new constant value to the right of the original value type.

    Declaration

    Swift

    func tupleRight<B>(_ b: B) -> Result<(Success, B), Failure>

    Parameters

    b

    Constant value for the tuple.

    Return Value

    A new value with the structure of the original value, with a tuple in its value type.

  • Lifts a value to the this context type.

    Declaration

    Swift

    static func pure(_ wrapped: Success) -> Result<Success, Failure>

    Parameters

    wrapped

    Value to be lifted.

    Return Value

    Provided value in this context type.

  • Creates a tuple out of two values in this context.

    Declaration

    Swift

    static func zip<A, B>(
        _ fa: Result<A, Failure>,
        _ fb: Result<B, Failure>
    ) -> Result<(A, B), Failure> where Success == (A, B)

    Parameters

    fa

    1st value of the tuple.

    fb

    2nd value of the tuple.

    Return Value

    A tuple in this context.

  • Creates a tuple out of three values in this context.

    Declaration

    Swift

    static func zip<A, B, C>(
        _ fa: Result<A, Failure>,
        _ fb: Result<B, Failure>,
        _ fc: Result<C, Failure>
    ) -> Result<(A, B, C), Failure> where Success == (A, B, C)

    Parameters

    fa

    1st value of the tuple.

    fb

    2nd value of the tuple.

    fc

    3rd value of the tuple.

    Return Value

    A tuple in this context.

  • Creates a tuple out of four values in this context.

    Declaration

    Swift

    static func zip<A, B, C, D>(
        _ fa: Result<A, Failure>,
        _ fb: Result<B, Failure>,
        _ fc: Result<C, Failure>,
        _ fd: Result<D, Failure>
    ) -> Result<(A, B, C, D), Failure> where Success == (A, B, C, D)

    Parameters

    a

    1st value of the tuple.

    b

    2nd value of the tuple.

    c

    3rd value of the tuple.

    d

    4th value of the tuple.

    Return Value

    A tuple in this context.

  • Creates a tuple out of five values in this context.

    Declaration

    Swift

    static func zip<A, B, C, D, E>(
        _ fa: Result<A, Failure>,
        _ fb: Result<B, Failure>,
        _ fc: Result<C, Failure>,
        _ fd: Result<D, Failure>,
        _ fe: Result<E, Failure>
    ) -> Result<(A, B, C, D, E), Failure> where Success == (A, B, C, D, E)

    Parameters

    a

    1st value of the tuple.

    b

    2nd value of the tuple.

    c

    3rd value of the tuple.

    d

    4th value of the tuple.

    e

    5th value of the tuple.

    Return Value

    A tuple in this context.

  • Creates a tuple out of six values in this context.

    Declaration

    Swift

    static func zip<A, B, C, D, E, F>(
        _ fa: Result<A, Failure>,
        _ fb: Result<B, Failure>,
        _ fc: Result<C, Failure>,
        _ fd: Result<D, Failure>,
        _ fe: Result<E, Failure>,
        _ ff: Result<F, Failure>
    ) -> Result<(A, B, C, D, E, F), Failure> where Success == (A, B, C, D, E, F)

    Parameters

    a

    1st value of the tuple.

    b

    2nd value of the tuple.

    c

    3rd value of the tuple.

    d

    4th value of the tuple.

    e

    5th value of the tuple.

    f

    6th value of the tuple.

    Return Value

    A tuple in this context.

  • Creates a tuple out of seven values in this context.

    Declaration

    Swift

    static func zip<A, B, C, D, E, F, G>(
        _ fa: Result<A, Failure>,
        _ fb: Result<B, Failure>,
        _ fc: Result<C, Failure>,
        _ fd: Result<D, Failure>,
        _ fe: Result<E, Failure>,
        _ ff: Result<F, Failure>,
        _ fg: Result<G, Failure>
    ) -> Result<(A, B, C, D, E, F, G), Failure> where Success == (A, B, C, D, E, F, G)

    Parameters

    a

    1st value of the tuple.

    b

    2nd value of the tuple.

    c

    3rd value of the tuple.

    d

    4th value of the tuple.

    e

    5th value of the tuple.

    f

    6th value of the tuple.

    g

    7th value of the tuple.

    Return Value

    A tuple in this context.

  • Creates a tuple out of eight values in this context.

    Declaration

    Swift

    static func zip<A, B, C, D, E, F, G, H>(
        _ fa: Result<A, Failure>,
        _ fb: Result<B, Failure>,
        _ fc: Result<C, Failure>,
        _ fd: Result<D, Failure>,
        _ fe: Result<E, Failure>,
        _ ff: Result<F, Failure>,
        _ fg: Result<G, Failure>,
        _ fh: Result<H, Failure>
    ) -> Result<(A, B, C, D, E, F, G, H), Failure> where Success == (A, B, C, D, E, F, G, H)

    Parameters

    a

    1st value of the tuple.

    b

    2nd value of the tuple.

    c

    3rd value of the tuple.

    d

    4th value of the tuple.

    e

    5th value of the tuple.

    f

    6th value of the tuple.

    g

    7th value of the tuple.

    h

    8th value of the tuple.

    Return Value

    A tuple in this context.

  • Creates a tuple out of nine values in this context.

    Declaration

    Swift

    static func zip<A, B, C, D, E, F, G, H, I>(
        _ fa: Result<A, Failure>,
        _ fb: Result<B, Failure>,
        _ fc: Result<C, Failure>,
        _ fd: Result<D, Failure>,
        _ fe: Result<E, Failure>,
        _ ff: Result<F, Failure>,
        _ fg: Result<G, Failure>,
        _ fh: Result<H, Failure>,
        _ fi: Result<I, Failure>
    ) -> Result<(A, B, C, D, E, F, G, H, I), Failure> where Success == (A, B, C, D, E, F, G, H, I)

    Parameters

    a

    1st value of the tuple.

    b

    2nd value of the tuple.

    c

    3rd value of the tuple.

    d

    4th value of the tuple.

    e

    5th value of the tuple.

    f

    6th value of the tuple.

    g

    7th value of the tuple.

    h

    8th value of the tuple.

    i

    9th value of the tuple.

    Return Value

    A tuple in this context.

  • Combines the result of two computations in this context, using the provided function.

    Declaration

    Swift

    static func map<A, B>(
        _ fa: Result<A, Failure>,
        _ fb: Result<B, Failure>,
        _ f: @escaping (A, B) -> Success
    ) -> Result<Success, Failure>

    Parameters

    fa

    1st computation.

    fb

    2nd computation.

    f

    Combination function.

    Return Value

    Result of combining the provided computations, in this context.

  • Combines the result of three computations in this context, using the provided function.

    Declaration

    Swift

    static func map<A, B, C>(
        _ fa: Result<A, Failure>,
        _ fb: Result<B, Failure>,
        _ fc: Result<C, Failure>,
        _ f: @escaping (A, B, C) -> Success
    ) -> Result<Success, Failure>

    Parameters

    fa

    1st computation.

    fb

    2nd computation.

    fc

    3rd computation.

    f

    Combination function.

    Return Value

    Result of combining the provided computations, in this context.

  • Combines the result of four computations in this context, using the provided function.

    Declaration

    Swift

    static func map<A, B, C, D>(
        _ fa: Result<A, Failure>,
        _ fb: Result<B, Failure>,
        _ fc: Result<C, Failure>,
        _ fd: Result<D, Failure>,
        _ f: @escaping (A, B, C, D) -> Success
    ) -> Result<Success, Failure>

    Parameters

    fa

    1st computation.

    fb

    2nd computation.

    fc

    3rd computation.

    fd

    4th computation.

    f

    Combination function.

    Return Value

    Result of combining the provided computations, in this context.

  • Combines the result of five computations in this context, using the provided function.

    Declaration

    Swift

    static func map<A, B, C, D, E>(
        _ fa: Result<A, Failure>,
        _ fb: Result<B, Failure>,
        _ fc: Result<C, Failure>,
        _ fd: Result<D, Failure>,
        _ fe: Result<E, Failure>,
        _ f: @escaping (A, B, C, D, E) -> Success
    ) -> Result<Success, Failure>

    Parameters

    fa

    1st computation.

    fb

    2nd computation.

    fc

    3rd computation.

    fd

    4th computation.

    fe

    5th computation.

    f

    Combination function.

    Return Value

    Result of combining the provided computations, in this context.

  • Combines the result of six computations in this context, using the provided function.

    Declaration

    Swift

    static func map<A, B, C, D, E, F>(
        _ fa: Result<A, Failure>,
        _ fb: Result<B, Failure>,
        _ fc: Result<C, Failure>,
        _ fd: Result<D, Failure>,
        _ fe: Result<E, Failure>,
        _ ff: Result<F, Failure>,
        _ f: @escaping (A, B, C, D, E, F) -> Success
    ) -> Result<Success, Failure>

    Parameters

    fa

    1st computation.

    fb

    2nd computation.

    fc

    3rd computation.

    fd

    4th computation.

    fe

    5th computation.

    ff

    6th computation.

    f

    Combination function.

    Return Value

    Result of combining the provided computations, in this context.

  • Combines the result of seven computations in this context, using the provided function.

    Declaration

    Swift

    static func map<A, B, C, D, E, F, G>(
        _ fa: Result<A, Failure>,
        _ fb: Result<B, Failure>,
        _ fc: Result<C, Failure>,
        _ fd: Result<D, Failure>,
        _ fe: Result<E, Failure>,
        _ ff: Result<F, Failure>,
        _ fg: Result<G, Failure>,
        _ f: @escaping (A, B, C, D, E, F, G) -> Success
    ) -> Result<Success, Failure>

    Parameters

    fa

    1st computation.

    fb

    2nd computation.

    fc

    3rd computation.

    fd

    4th computation.

    fe

    5th computation.

    ff

    6th computation.

    fg

    7th computation.

    f

    Combination function.

    Return Value

    Result of combining the provided computations, in this context.

  • Combines the result of eight computations in this context, using the provided function.

    Declaration

    Swift

    static func map<A, B, C, D, E, F, G, H>(
        _ fa: Result<A, Failure>,
        _ fb: Result<B, Failure>,
        _ fc: Result<C, Failure>,
        _ fd: Result<D, Failure>,
        _ fe: Result<E, Failure>,
        _ ff: Result<F, Failure>,
        _ fg: Result<G, Failure>,
        _ fh: Result<H, Failure>,
        _ f: @escaping (A, B, C, D, E, F, G, H) -> Success
    ) -> Result<Success, Failure>

    Parameters

    fa

    1st computation.

    fb

    2nd computation.

    fc

    3rd computation.

    fd

    4th computation.

    fe

    5th computation.

    ff

    6th computation.

    fg

    7th computation.

    fh

    8th computation.

    f

    Combination function.

    Return Value

    Result of combining the provided computations, in this context.

  • Combines the result of nine computations in this context, using the provided function.

    Declaration

    Swift

    static func map<A, B, C, D, E, F, G, H, I>(
        _ fa: Result<A, Failure>,
        _ fb: Result<B, Failure>,
        _ fc: Result<C, Failure>,
        _ fd: Result<D, Failure>,
        _ fe: Result<E, Failure>,
        _ ff: Result<F, Failure>,
        _ fg: Result<G, Failure>,
        _ fh: Result<H, Failure>,
        _ fi: Result<I, Failure>,
        _ f: @escaping (A, B, C, D, E, F, G, H, I) -> Success
    ) -> Result<Success, Failure>

    Parameters

    fa

    1st computation.

    fb

    2nd computation.

    fc

    3rd computation.

    fd

    4th computation.

    fe

    5th computation.

    ff

    6th computation.

    fg

    7th computation.

    fh

    8th computation.

    fi

    9th computation.

    f

    Combination function.

    Return Value

    Result of combining the provided computations, in this context.

  • Sequential application.

    Declaration

    Swift

    func ap<A, B>(_ fa: Result<A, Failure>) -> Result<B, Failure> where Success == (A) -> B

    Parameters

    fa

    A value in the context implementing this instance.

    Return Value

    A value in this context, resulting from the transformation of the contained original value with the contained function.

  • Sequentially compose two computations, discarding the value produced by the first.

    Declaration

    Swift

    func zipRight<B>(_ fb: Result<B, Failure>) -> Result<B, Failure>

    Parameters

    fb

    2nd computation.

    Return Value

    Result of running the second computation after the first one.

  • Sequentially compose two computations, discarding the value produced by the second.

    Declaration

    Swift

    func zipLeft<B>(_ fb: Result<B, Failure>) -> Result<Success, Failure>

    Parameters

    fb

    2nd computation.

    Return Value

    Result produced from the first computation after both are computed.

  • Flattens this nested structure into a single layer.

    Declaration

    Swift

    func flatten<A>() -> Result<A, Failure> where Success == Result<A, Failure>

    Return Value

    Value with a single context structure.

  • Sequentially compose two computations, discarding the value produced by the first.

    Declaration

    Swift

    func followedBy<A>(_ fa: Result<A, Failure>) -> Result<A, Failure>

    Parameters

    fa

    2nd computation.

    Return Value

    Result of running the second computation after the first one.

  • Sequentially compose two computations, discarding the value produced by the second.

    Declaration

    Swift

    func forEffect<A>(_ fa: Result<A, Failure>) -> Result<Success, Failure>

    Parameters

    fa

    2nd computation.

    Return Value

    Result produced from the first computation after both are computed.

  • Pair the result of a computation with the result of applying a function to such result.

    Declaration

    Swift

    func mproduct<A>(_ f: @escaping (Success) -> Result<A, Failure>) -> Result<(Success, A), Failure>

    Parameters

    f

    A function to be applied to the result of the computation.

    Return Value

    A tuple of the result of the computation paired with the result of the function, in this context.

  • Conditionally apply a closure based on the boolean result of this computation.

    Declaration

    Swift

    func `if`<A>(
        then f: @escaping () -> Result<A, Failure>,
        else g: @escaping () -> Result<A, Failure>
    ) -> Result<A, Failure> where Success == Bool

    Parameters

    then

    Closure to be applied if the computation evaluates to true.

    else

    Closure to be applied if the computation evaluates to false.

    Return Value

    Result of applying the corresponding closure based on the result of the computation.

  • Applies a monadic function and discard the result while keeping the effect.

    Declaration

    Swift

    func flatTap<A>(_ f: @escaping (Success) -> Result<A, Failure>) -> Result<Success, Failure>

    Parameters

    f

    A monadic function which result will be discarded.

    Return Value

    A computation with the result of the initial computation and the effect caused by the function application.

  • Lifts an error to this context.

    Declaration

    Swift

    static func raiseError(_ error: Failure) -> Result<Success, Failure>

    Parameters

    error

    A value of the error type.

    Return Value

    A value representing the error in this context.

  • Creates a value of this type from an Either.

    Declaration

    Swift

    static func from(either: Either<Failure, Success>) -> Result<Success, Failure>

    Parameters

    either

    Either value to convert to this type.

    Return Value

    A value that represents the same content from Either, in this context.

  • Handles an error, potentially recovering from it by mapping it to a value in this context.

    Declaration

    Swift

    func handleErrorWith(_ f: @escaping (Failure) -> Result<Success, Failure>) -> Result<Success, Failure>

    Parameters

    f

    A recovery function.

    Return Value

    A value where the possible errors have been recovered using the provided function.

  • Handles an error, potentially recovering from it by mapping it to a value.

    Declaration

    Swift

    func handleError(_ f: @escaping (Failure) -> Success) -> Result<Success, Failure>

    Parameters

    f

    A recovery function.

    Return Value

    A value where the possible errors have been recovered using the provided function.