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
Staterepresents neither a state nor a (value, state) pairit wraps a transformation old_state -> (value, new_state)
the
runmethod is this wrapped transformationbindis just state propagating function composition
- __init__(run: Callable[[S], tuple[A, S]]) None¶
- bind(g: Callable[[A], State[S, B]]) State[S, B]¶
Perform function composition while propagating state.
- eval(init: S) A¶
Evaluate the Monad via passing an initial state.
- map2(sb: State[S, B], f: Callable[[A, B], C]) State[S, C]¶
Map a function of two variables over two state actions.
- 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
- 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