Kleisli

public final class Kleisli<F, D, A> : KleisliOf<F, D, A>

Kleisli represents a function with the signature (D) -> Kind<F, A>.

  • Safe downcast.

    Declaration

    Swift

    public static func fix(_ fa: KleisliOf<F, D, A>) -> Kleisli<F, D, A>

    Parameters

    fa

    Value in the higher-kind form.

    Return Value

    Value cast to Kleisli.

  • Creates a constant Kleisli function.

    Declaration

    Swift

    public static func liftF(_ fa: Kind<F, A>) -> Kleisli<F, D, A>

    Parameters

    fa

    Constant value to return.

    Return Value

    A constant Kleisli function.

  • Initializes a Kleisli value.

    Declaration

    Swift

    public init(_ f: @escaping (D) -> Kind<F, A>)

    Parameters

    run

    Closure to be wrapped in this Kleisli.

  • Inkoves this Kleisli function with an input value.

    Declaration

    Swift

    public func run(_ value: D) -> Kind<F, A>

    Parameters

    value

    Input to the function.

    Return Value

    Output of the Kleisli.

  • Inkoves this Kleisli function with an input value.

    Declaration

    Swift

    public func callAsFunction(_ value: D) -> Kind<F, A>

    Parameters

    value

    Input to the function.

    Return Value

    Output of the Kleisli.

  • Pre-composes this Kleisli function with a function transforming the input type.

    Declaration

    Swift

    public func contramap<DD>(_ f: @escaping (DD) -> D) -> Kleisli<F, DD, A>

    Parameters

    f

    Transforming function.

    Return Value

    Composition of the two functions.

  • Narrows the scope of the context of this Kleisli from Any to a concrete type

    Declaration

    Swift

    public func narrow<DD>() -> Kleisli<F, DD, A> where D == Any

    Return Value

    A copy of this Kleisli working on a more precise context.

  • Transforms the result of this Kleisli function.

    Declaration

    Swift

    public func transformT<G, B>(_ f: @escaping (Kind<F, A>) -> Kind<G, B>) -> Kleisli<G, D, B>

    Parameters

    f

    Transforming function.

    Return Value

    A Kleisli function that behaves as the original one, with its result transformed.

  • Creates an EnvIO from a side-effectful function that has a dependency.

    Declaration

    Swift

    static func invoke<E: Error>(_ f: @escaping (D) throws -> A) -> EnvIO<D, E, A>
        where F == IOPartial<E>

    Parameters

    f

    Side-effectful function. Errors thrown from this function must be of type E; otherwise, a fatal error will happen.

    Return Value

    An EnvIO value suspending the execution of the side effect.

  • Creates an EnvIO from a side-effectful function returning an Either that has a dependency.

    Declaration

    Swift

    static func invokeEither<E: Error>(_ f: @escaping (D) -> Either<E, A>) -> EnvIO<D, E, A>
        where F == IOPartial<E>

    Parameters

    f

    Side-effectful function.

    Return Value

    An EnvIO value suspending the execution of the side effect.

  • Creates an EnvIO from a side-effectful function returning a Result that has a dependency.

    Declaration

    Swift

    static func invokeResult<E: Error>(_ f: @escaping (D) -> Result<A, E>) -> EnvIO<D, E, A>
        where F == IOPartial<E>

    Parameters

    f

    Side-effectful function.

    Return Value

    An EnvIO value suspending the execution of the side effect.

  • Creates an EnvIO from a side-effectful function returning a Validated that has a dependency.

    Declaration

    Swift

    static func invokeValidated<E: Error>(_ f: @escaping (D) -> Validated<E, A>) -> EnvIO<D, E, A>
        where F == IOPartial<E>

    Parameters

    f

    Side-effectful function.

    Return Value

    An EnvIO value suspending the execution of the side effect.

  • Transforms the error type of this EnvIO

    Declaration

    Swift

    func mapError<E: Error, EE: Error>(_ f: @escaping (E) -> EE) -> EnvIO<D, EE, A>
        where F == IOPartial<E>

    Parameters

    f

    Function transforming the error.

    Return Value

    An EnvIO value with the new error type.

  • Provides the required environment.

    Declaration

    Swift

    func provide<E: Error>(_ d: D) -> IO<E, A>
        where F == IOPartial<E>

    Parameters

    d

    Environment.

    Return Value

    An IO resulting from running this computation with the provided environment.

  • Retries this computation if it fails based on the provided retrial policy.

    This computation will be at least executed once, and if it fails, it will be retried according to the policy.

    Declaration

    Swift

    func retry<S, O, E: Error>(_ policy: Schedule<D, E, S, O>) -> EnvIO<D, E, A>
        where F == IOPartial<E>

    Parameters

    policy

    Retrial policy.

    Return Value

    A computation that is retried based on the provided policy when it fails.

  • Retries this computation if it fails based on the provided retrial policy, providing a default computation to handle failures after retrial.

    This computation will be at least executed once, and if it fails, it will be retried according to the policy.

    Declaration

    Swift

    func retry<S, O, B, E: Error>(
        _ policy: Schedule<D, E, S, O>,
        orElse: @escaping (E, O) -> EnvIO<D, E, B>) -> EnvIO<D, E, Either<B, A>>
        where F == IOPartial<E>

    Parameters

    policy

    Retrial policy.

    orElse

    Function to handle errors after retrying.

    Return Value

    A computation that is retried based on the provided policy when it fails.

  • Repeats this computation until the provided repeating policy completes, or until it fails.

    This computation will be at least executed once, and if it succeeds, it will be repeated additional times according to the policy.

    Declaration

    Swift

    func `repeat`<S, O, E: Error>(
        _ policy: Schedule<D, A, S, O>,
        onUpdateError: @escaping () -> E = { fatalError("Impossible to update error on repeat.") }) -> EnvIO<D, E, O>
        where F == IOPartial<E>

    Parameters

    policy

    Repeating policy.

    onUpdateError

    A function providing an error in case the policy fails to update properly.

    Return Value

    A computation that is repeated based on the provided policy when it succeeds.

  • Repeats this computation until the provided repeating policy completes, or until it fails, with a function to handle potential failures.

    Declaration

    Swift

    func `repeat`<S, O, B, E: Error>(
        _ policy: Schedule<D, A, S, O>,
        onUpdateError: @escaping () -> E = { fatalError("Impossible to update error on repeat.") },
        orElse: @escaping (E, O?) -> EnvIO<D, E, B>) -> EnvIO<D, E, Either<B, O>>
        where F == IOPartial<E>

    Parameters

    policy

    Repeating policy.

    onUpdateError

    A function providing an error in case the policy fails to update properly.

    orElse

    A function to return a computation in case of error.

    Return Value

    A computation that is repeated based on the provided policy when it succeeds.

  • Transforms the type arguments of this EnvIO.

    Declaration

    Swift

    func bimap<E: Error, EE: Error, B>(
        _ fe: @escaping (E) -> EE,
        _ fa: @escaping (A) -> B) -> EnvIO<D, EE, B>
        where F == IOPartial<E>

    Parameters

    fe

    Function to transform the error type argument.

    fa

    Function to transform the output type argument.

    Return Value

    An EnvIO with both type arguments transformed.

  • Performs the side effects that are suspended in this IO in a synchronous manner.

    Throws

    Error of type E that may happen during the evaluation of the side-effects. Errors of other types thrown from the evaluation of this IO will cause a fatal error.

    Declaration

    Swift

    func unsafeRunSync<E: Error>(
        with d: D,
        on queue: DispatchQueue = .main) throws -> A
        where F == IOPartial<E>

    Parameters

    d

    Dependencies needed in this operation.

    queue

    Dispatch queue used to execute the side effects. Defaults to the main queue.

    Return Value

    Value produced after running the suspended side effects.

  • Performs the side effects that are suspended in this EnvIO in a synchronous manner.

    Declaration

    Swift

    func unsafeRunSyncEither<E: Error>(
        with d: D,
        on queue: DispatchQueue = .main) -> Either<E, A>
        where F == IOPartial<E>

    Parameters

    d

    Dependencies needed in this operation.

    queue

    Dispatch queue used to execute the side effects. Defaults to the main queue.

    Return Value

    An Either wrapping errors in the left side and values on the right side. Errors of other types thrown from the evaluation of this IO will cause a fatal error.

  • Performs the side effects that are suspended in this EnvIO in an asynchronous manner.

    Declaration

    Swift

    func unsafeRunAsync<E: Error>(
        with d: D,
        on queue: DispatchQueue = .main,
        _ callback: @escaping Callback<E, A> = { _ in })
        where F == IOPartial<E>

    Parameters

    d

    Dependencies needed in this operation.

    queue

    Dispatch queue used to execute the side effects. Defaults to the main queue.

    callback

    A callback function to receive the results of the evaluation. Errors of other types thrown from the evaluation of this IO will cause a fatal error.

  • Creates an EnvIO from a side-effectful function returning a Try that has a dependency.

    Declaration

    Swift

    static func invokeTry(_ f: @escaping (D) -> Try<A>) -> EnvIO<D, Error, A>

    Parameters

    f

    Side-effectful function.

    Return Value

    An EnvIO value suspending the execution of the side effect.

  • Performs the side effects that are suspended in this IO in a synchronous manner.

    Throws

    Error of type E that may happen during the evaluation of the side-effects. Errors of other types thrown from the evaluation of this IO will cause a fatal error.

    Declaration

    Swift

    func unsafeRunSync<E: Error>(on queue: DispatchQueue = .main) throws -> A
        where F == IOPartial<E>

    Parameters

    queue

    Dispatch queue used to execute the side effects. Defaults to the main queue.

    Return Value

    Value produced after running the suspended side effects.

  • Performs the side effects that are suspended in this EnvIO in a synchronous manner.

    Declaration

    Swift

    func unsafeRunSyncEither<E: Error>(on queue: DispatchQueue = .main) -> Either<E, A>
        where F == IOPartial<E>

    Parameters

    queue

    Dispatch queue used to execute the side effects. Defaults to the main queue.

    Return Value

    An Either wrapping errors in the left side and values on the right side. Errors of other types thrown from the evaluation of this IO will cause a fatal error.

  • Performs the side effects that are suspended in this EnvIO in an asynchronous manner.

    Declaration

    Swift

    func unsafeRunAsync<E: Error>(
        on queue: DispatchQueue = .main,
        _ callback: @escaping Callback<E, A> = { _ in })
        where F == IOPartial<E>

    Parameters

    queue

    Dispatch queue used to execute the side effects. Defaults to the main queue.

    callback

    A callback function to receive the results of the evaluation. Errors of other types thrown from the evaluation of this IO will cause a fatal error.

  • Sleep for the specified amount of time.

    Declaration

    Swift

    static func sleep<E: Error>(_ interval: DispatchTimeInterval) -> EnvIO<D, E, Void>
        where F == IOPartial<E>

    Parameters

    interval

    Time to sleep.

    Return Value

    A computation that sleeps for the specified amount of time.

  • Sleep for the specified amount of time.

    Declaration

    Swift

    static func sleep<E: Error>(_ interval: TimeInterval) -> EnvIO<D, E, Void>
        where F == IOPartial<E>

    Parameters

    interval

    Time to sleep.

    Return Value

    A computation that sleeps for the specified amount of time.

  • Accesses the environment to produce a pure value.

    Declaration

    Swift

    public static func access(_ f: @escaping (D) -> A) -> Kleisli<F, D, A>

    Parameters

    f

    Function accessing the environment.

    Return Value

    A Kleisli function wrapping the produced value.

  • Accesses the environment to produce a Kleisli effect.

    Declaration

    Swift

    public static func accessM(_ f: @escaping (D) -> Kleisli<F, D, A>) -> Kleisli<F, D, A>

    Parameters

    f

    Function accessing the environment.

    Return Value

    A Kleisli function wraping the produced value.

  • Zips this Kleisli function with another one with the same input type.

    Declaration

    Swift

    public func zip<B>(_ o: Kleisli<F, D, B>) -> Kleisli<F, D, (A, B)>

    Parameters

    o

    Kleisli function to be zipped with this one.

    Return Value

    A Kleisli function that pairs the output of the two Kleisli functions zipped in this operation.

  • Composes this Kleisli with another one.

    Declaration

    Swift

    public func andThen<C>(_ f: Kleisli<F, A, C>) -> Kleisli<F, D, C>

    Parameters

    f

    Kleisli function to be composed after the this one.

    Return Value

    A Kleisli function that is equivalent to running this Kleisli and then the received one.

  • Composes this Kleisli with a function in Kleisli form.

    Declaration

    Swift

    public func andThen<B>(_ f: @escaping (A) -> Kind<F, B>) -> Kleisli<F, D, B>

    Parameters

    f

    A function to be composed after this Kleisli.

    Return Value

    A Kleisli function that is equivalent to running this Kleisli and then the received one.

  • Composes this Kleisli with a constant value.

    Declaration

    Swift

    public func andThen<B>(_ fb: Kind<F, B>) -> Kleisli<F, D, B>

    Parameters

    fb

    Constant value.

    Return Value

    A Kleisli function that is equivalent to running this Kleisli and ommitting its result, returning the constant value produced.

  • Folds over the result of this computation by accepting a function to execute in case of error, and another one in the case of success.

    Declaration

    Swift

    public func foldA<B>(
        _ f: @escaping (F.E) -> B,
        _ g: @escaping (A) -> B
    ) -> Kleisli<F, D, B>

    Parameters

    f

    Function to run in case of error.

    g

    Function to run in case of success.

    Return Value

    A computation from the result of applying the provided functions to the result of this computation.

  • Folds over the result of this computation by accepting an effect to execute in case of error, and another one in the case of success.

    Declaration

    Swift

    public func foldM<B>(
        _ f: @escaping (F.E) -> Kleisli<F, D, B>,
        _ g: @escaping (A) -> Kleisli<F, D, B>) -> Kleisli<F, D, B>

    Parameters

    f

    Function to run in case of error.

    g

    Function to run in case of success.

    Return Value

    A computation from the result of applying the provided functions to the result of this computation.