StateT

public final class StateT<F, S, A> : StateTOf<F, S, A>

StateT transformer represents operations that need to write and read a state through a computation or effect.

Some computations may not require the full power of this transformer: - For read-only state, see ReaderT / Kleisli. - To accumulate a value without using it on the way, see WriterT.

  • Safe downcast.

    Declaration

    Swift

    public static func fix(_ fa: StateTOf<F, S, A>) -> StateT<F, S, A>

    Parameters

    fa

    Value in the higher-kind form.

    Return Value

    Value cast to StateT.

  • Initializes a StateT.

    Declaration

    Swift

    public init(_ runF: @escaping (S) -> Kind<F, (S, A)>)

    Parameters

    runF

    An effect describing a function that receives a state and produces an effect that updates the state and productes a value.

  • Generalizes this StateT to a parent state, given a lens to focus from the parent to the child state.

    Declaration

    Swift

    func focus<SS>(_ lens: Lens<SS, S>) -> StateT<F, SS, A>

    Parameters

    lens

    A Lens to focus from the parent state into the child state.

    Return Value

    An StateT that produces the same computation but updates the state in a parent state.

  • Initializes a State value.

    Declaration

    Swift

    convenience init(_ run: @escaping (S) -> (S, A))

    Parameters

    run

    A function that depends on a state and produces a new state and a value.

  • Runs this computation provided an initial state.

    Declaration

    Swift

    func run(_ initialState: S) -> (S, A)

    Parameters

    initialState

    Initial state for this computation.

    Return Value

    A pair with the updated state and the produced value.

  • Runs this computation provided an initial state.

    Declaration

    Swift

    func runA(_ s: S) -> A

    Parameters

    s

    Initial state for this computation.

    Return Value

    Produced value from this computation.

  • Runs this computation provided an initial state.

    Declaration

    Swift

    func runS(_ s: S) -> S

    Parameters

    s

    Initial state for this computation.

    Return Value

    Updated state after running the computation.

  • Transforms the return value and final state of a computation using a provided function.

    Declaration

    Swift

    public func transform<B>(_ f: @escaping (S, A) -> (S, B)) -> StateT<F, S, B>

    Parameters

    f

    Transforming function.

    Return Value

    An StateT where the final state and produced value have been transformed using the provided function.

  • Transforms the wrapped value using a provided function.

    Declaration

    Swift

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

    Parameters

    f

    Transforming function.

    Return Value

    An StateT where the final state and produced value have been transformed using the provided function.

  • Generalizes this StateT to a parent state, given functions to get and set the inner state into the general state.

    Declaration

    Swift

    public func focus<SS>(_ getter: @escaping (SS) -> S, _ setter: @escaping (SS, S) -> SS) -> StateT<F, SS, A>

    Parameters

    getter

    Function to get the state from the parent.

    setter

    Function to set the state into the parent.

    Return Value

    An StateT that produces the same computation but updates the state in a parent state.

  • Lifts an effect by wrapping the contained value into a function that depends on some state.

    Declaration

    Swift

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

    Parameters

    fa

    Value to be lifted.

    Return Value

    A StateT that produces the contained value in the original effect, preserving the state.

  • Runs this computation using the provided initial state.

    Declaration

    Swift

    public func runA(_ s: S) -> Kind<F, A>

    Parameters

    s

    Initial state to run this computation.

    Return Value

    Result of running this computation with the provided state, wrapped in the effect.

  • Runs this computation using the provided initial state.

    Declaration

    Swift

    public func runS(_ s: S) -> Kind<F, S>

    Parameters

    s

    Initial state to run this computation.

    Return Value

    New state after running this computation with the provided state, wrapped in the effect.

  • Runs this computation using the provided initial state.

    Declaration

    Swift

    public func runM(_ initial: S) -> Kind<F, (S, A)>

    Parameters

    initial

    Initial state to run this computation.

    Return Value

    A pair with the new state and produced value, wrapped in the effect.

  • Modifies the state with a function and returns unit.

    Declaration

    Swift

    public func modifyF(_ f: @escaping (S) -> Kind<F, S>) -> StateT<F, S, ()>

    Parameters

    f

    Function to modify the state.

    Return Value

    A StateT value with a modified state and unit as result value.

  • Sets the state to a specific value and returns unit.

    Declaration

    Swift

    public func setF(_ fs: Kind<F, S>) -> StateT<F, S, ()>

    Parameters

    fs

    Value to set the state.

    Return Value

    A StateT value with a modified state and unit as result value.

  • Flatmaps a function that produces an effect and lifts if back to StateT.

    Declaration

    Swift

    public func semiflatMap<B>(_ f: @escaping (A) -> Kind<F, B>) -> StateT<F, S, B>

    Parameters

    f

    A function producing an effect.

    Return Value

    Result of flatmapping and lifting the function to this value.