fptools.state

class pythonic_fp.fptools.state.State(run: Callable[[S], tuple[A, S]])

State Monad

Data structure generating values while propagating changes of state. A pure FP implementation for the State Monad

  • class State represents neither a state nor (value, state) pair

    • it wraps a transformation old_state -> (value, new_state)

    • the run method is this wrapped transformation

    • bind is just state propagating function composition

bind(g: Callable[[A], State[S, B]]) State[S, B]

Perform function composition while propagating state.

both(rb: State[S, B]) State[S, tuple[A, B]]

Return a tuple of two state actions.

eval(init: S) A

Evaluate the Monad via passing an initial state.

static get() State

Set run action to return the current state

  • the current state is propagated unchanged

  • current value now set to current state

  • will need type annotation

map(f: Callable[[A], B]) State[S, B]

Map a function over a run action.

map2(sb: State[S, B], f: Callable[[A, B], C]) State[S, C]

Map a function of two variables over two state actions.

static modify(f: Callable[[ST], ST]) State[ST, tuple[()]]

Modify previous state.

  • like put, but modify previous state via f

  • will need type annotation

    • mypy has no “a priori” way to know what ST is

static put(s: ST) State[ST, tuple[()]]

Manually insert a state.

  • ignores previous state and swaps in a new state

  • assigns a canonically meaningless value for current value

static sequence(sas: list[State]) State[ST, list[AA]]

Combine a list of state actions into a state action of a list.

  • all state actions must be of the same type

  • run method evaluates list front to back

static unit(b: B) State

Create a State action returning the given value.