MonadState

public protocol MonadState : Monad

A MonadState is a Monad that maintains a state.

  • Type of the state maintained in this instance

    Declaration

    Swift

    associatedtype S
  • Retrieves the state from the internals of the monad.

    Declaration

    Swift

    static func get() -> Kind<Self, S>

    Return Value

    Maintained state.

  • Replaces the state inside the monad.

    Declaration

    Swift

    static func set(_ s: S) -> Kind<Self, ()>

    Parameters

    s

    New state.

    Return Value

    Unit.

  • state(_:) Extension method

    Embeds a state action into the monad.

    Declaration

    Swift

    static func state<A>(_ f: @escaping (S) -> (S, A)) -> Kind<Self, A>

    Parameters

    f

    A function that receives the state and computes a value and a new state.

    Return Value

    A value with the output of the function and the new state.

  • modify(_:) Extension method

    Modifies the internal state.

    Declaration

    Swift

    static func modify(_ f: @escaping (S) -> S) -> Kind<Self, ()>

    Parameters

    f

    Function that modifies the state.

    Return Value

    Unit.

  • inspect(_:) Extension method

    Retrieves a specific component of the state.

    Declaration

    Swift

    static func inspect<A>(_ f: @escaping (S) -> A) -> Kind<Self, A>

    Parameters

    f

    Projection function to obtain part of the state.

    Return Value

    A specific part of the state.