state monad¶
- final class pythonic_fp.fptools.state.State¶
Bases:
GenericState monad
A pure FP implementation of the State Monad, a data structure generating values while propagating changes of state.
Note
A monad is a value in a context. The State monad wraps neither a state nor a (value, state) pair. It wraps a transformation
old_state -> (value, new_state)called a “state action”.Class State
Instance members:
Property
runis the state actionMethod
bindperforms state action compositionMethod
evalis the run actionthe run action evaluates the state action by
supplying an initial state
returning the resulting value
Static members:
Method
unitcreates aStatewhose run action returns a given constant value.Method
getcreates aStatewhose run action returns the current state.Method
setcreates aStatewhich ignores the old state and swaps in a new one.Method
modifycreates aStatewhich modifies the previous state via a function.Method
sequencecombine a list ofStatesinto aStatewhose run action returns the list of generated values.
- __init__(run: Callable[[S], tuple[A, S]]) None¶
initialize
- param run:
State action.
- bind(g: Callable[[A], State[S, B]]) State[S, B]¶
state action composition
- param g:
A function that produces a
State[S, B]from anA.- returns:
A
State[S, B]whose state action is the composition of the state actions fromselffollowed by the one produced byg.
- eval(init: S) A¶
run action
Evaluate the state action by passing in an initial state and returning the produced value.
- param init:
An initial state to pass into the state action.
- returns:
The value produced by the run action.
- map(f: Callable[[A], B]) State[S, B]¶
map
Map function
fover the resulting value of a state action propagating the current state.- param f:
Function to map.
- returns:
A State whose run action produces
f(a)whereais the value produced by the run action ofself.
- map2(sb: State[S, B], f: Callable[[A, B], C]) State[S, C]¶
map2
Combine two state monads,
selfandsb, with a function. Resulting run action just propagates the current state.- param sb:
Stateto combine withself.- param f:
Function used by the resulting run action on the values produced by the run actions of
selfandsbfrom the same initial state.
- both(rb: State[S, B]) State[S, tuple[A, B]]¶
both
Return a
Statewhose run action returns a tuple from from the run actions from .- param sb:
Stateto combine withselffor the second element of tuple produced by run action.
- static unit(b: B) State¶
unit
Create a State whose run action returns the given constant
band propagate the present state.- param b:
Value the new State’s run action will return.
- returns:
A new
State[ST, B]from a valueb: B.
- static get() State¶
get state
Set run action to return the current state and propagate it unchanged.
the current state is propagated unchanged
current value now set to current state
will need type annotation
- returns:
A state monad wrapping a state action to return the current state. Propagates the current state unchanged.
- static put(s: ST) State[ST, tuple[()]]¶
put state
Manually insert a new state.
ignores previous state and swaps in a new state
resulting run action will return an empty tuple
the traditional canonically meaningless value in FP
- param s:
The state to swap in for current state
- returns:
State monad wrapping a state action which ignores any initial state passed in when evaluated.
- static modify(f: Callable[[ST], ST]) State[ST, tuple[()]]¶
modify
Modify previous state with a function. Like put, but modify previous state via
f.- param f:
Function to modify the current state.
- returns:
A State monad with a modified state.
Note
Will need type annotation. Static type checkers like mypy have no a priori knowledge of what
STcould be.
- static sequence(sas: list[State]) State[ST, list[AA]]¶
sequence a list
Combine a list of state monads into a state monad whose run action returns a list of the values returned by each run action from the original list.
- param sas:
A list of state monads all of type
State[ST, AA].- returns:
A state monad of type
State[ST, list[AA]].
Note
The run action evaluates the run actions of the list front to back. The state is also propagated, not necessarily unchanged, front to back.