Monad

public protocol Monad : Selective

A Monad provides functionality to sequence operations that are dependent from one another.

Instances of Monad must obey the following rules:

flatMap(pure(a), f) == f(a)
flatMap(fa, pure) == fa
flatMap(fa) { a in flatMap(f(a), g) } == flatMap(flatMap(fa, f), g)

Also, instances of Monad derive a default implementation for Applicative.ap as:

ap(ff, fa) == flapMap(ff, { f in map(fa, f) }
  • Sequentially compose two computations, passing any value produced by the first as an argument to the second.

    Declaration

    Swift

    static func flatMap<A, B>(
        _ fa: Kind<Self, A>,
        _ f: @escaping (A) -> Kind<Self, B>) -> Kind<Self, B>

    Parameters

    fa

    First computation.

    f

    A function describing the second computation, which depends on the value of the first.

    Return Value

    Result of composing the two computations.

  • Monadic tail recursion.

    tailRecM can be used for computations that can potentially make the stack overflow.

    Initially introduced in Stack Safety for Free

    Declaration

    Swift

    static func tailRecM<A, B>(
        _ a: A,
        _ f: @escaping (A) -> Kind<Self, Either<A, B>>) -> Kind<Self, B>

    Parameters

    a

    Initial value for the recursion.

    f

    A function describing a recursive step.

    Return Value

    Result of evaluating recursively the provided function with the initial value.

  • ap(_:_:) Extension method

    Declaration

    Swift

    static func ap<A, B>(
        _ ff: Kind<Self, (A) -> B>,
        _ fa: Kind<Self, A>) -> Kind<Self, B>
  • select(_:_:) Extension method

    Declaration

    Swift

    static func select<A, B>(
        _ fab: Kind<Self, Either<A, B>>,
        _ f: Kind<Self, (A) -> B>) -> Kind<Self, B>
  • flatten(_:) Extension method

    Flattens a nested structure of the context implementing this instance into a single layer.

    Declaration

    Swift

    static func flatten<A>(_ ffa: Kind<Self, Kind<Self, A>>) -> Kind<Self, A>

    Parameters

    ffa

    Value with a nested structure.

    Return Value

    Value with a single context structure.

  • followedBy(_:_:) Extension method

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

    Declaration

    Swift

    static func followedBy<A, B>(
        _ fa: Kind<Self, A>,
        _ fb: Kind<Self, B>) -> Kind<Self, B>

    Parameters

    fa

    1st computation.

    fb

    2nd computation.

    Return Value

    Result of running the second computation after the first one.

  • followedByEval(_:_:) Extension method

    Sequentially compose a computation with a potentially lazy one, discarding the value produced by the first.

    Declaration

    Swift

    static func followedByEval<A, B>(
        _ fa: Kind<Self, A>,
        _ fb: Eval<Kind<Self, B>>) -> Kind<Self, B>

    Parameters

    fa

    Regular computation.

    fb

    Potentially lazy computation.

    Return Value

    Result of running the second computation after the first one.

  • forEffect(_:_:) Extension method

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

    Declaration

    Swift

    static func forEffect<A, B>(
        _ fa: Kind<Self, A>,
        _ fb: Kind<Self, B>) -> Kind<Self, A>

    Parameters

    fa

    1st computation.

    fb

    2nd computation.

    Return Value

    Result produced from the first computation after both are computed.

  • forEffectEval(_:_:) Extension method

    Sequentially compose a computation with a potentially lazy one, discarding the value produced by the second.

    Declaration

    Swift

    static func forEffectEval<A, B>(
        _ fa: Kind<Self, A>,
        _ fb: Eval<Kind<Self, B>>) -> Kind<Self, A>

    Parameters

    fa

    Regular computation.

    fb

    Potentially lazy computation.

    Return Value

    Result produced from the first computation after both are computed.

  • mproduct(_:_:) Extension method

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

    Declaration

    Swift

    static func mproduct<A, B>(
        _ fa: Kind<Self, A>,
        _ f: @escaping (A) -> Kind<Self, B>) -> Kind<Self, (A, B)>

    Parameters

    fa

    A computation in the context implementing this instance.

    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 the context implementing this instance.

  • ifM(_:_:_:) Extension method

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

    Declaration

    Swift

    static func ifM<B>(
        _ fa: Kind<Self, Bool>,
        _ ifTrue: @escaping () -> Kind<Self, B>,
        _ ifFalse: @escaping () -> Kind<Self, B>) -> Kind<Self, B>

    Parameters

    fa

    A boolean computation.

    ifTrue

    Closure to be applied if the computation evaluates to true.

    ifFalse

    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.

  • flatTap(_:_:) Extension method

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

    Declaration

    Swift

    static func flatTap<A, B>(
        _ fa: Kind<Self, A>,
        _ f: @escaping (A) -> Kind<Self, B>) -> Kind<Self, A>

    Parameters

    fa

    A computation.

    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.

  • stackSafeFlatMap(_:_:) Extension method

    A stack safe version of flatMap, based on tailRecM.

    Declaration

    Swift

    static func stackSafeFlatMap<A, B>(
        _ fa: Kind<Self, A>,
        _ f: @escaping (A) -> Kind<Self, B>) -> Kind<Self, B>

    Parameters

    fa

    First computation.

    f

    A function describing the second computation, which depends on the value of the first.

    Return Value

    Result of composing the two computations.

  • var(_:) Extension method

    Creates a bound variable in this monadic context for the specified type.

    Declaration

    Swift

    static func `var`<A>(_ type: A.Type) -> BoundVar<Self, A>

    Parameters

    type

    Type of the variable.

    Return Value

    A bound variable.