StateT
funstruct.experimental.monadtransformer.state_t
StateT — state monad transformer over any monad.
Examples:
>>> from funstruct.experimental.monadtransformer import StateT
>>> from funstruct.monad.either import Either, Right, Left
>>> inc = StateT(lambda s: Right((s + 1, s)))
>>> inc.run(0)
Right((1, 0))
>>> StateT.pure(42, Either).run(0)
Right((0, 42))
>>> inc.then(inc).run(0)
Right((2, 1))
StateT
Bases: MonadTransformer, Generic[_F, _A]
Generic state transformer: S -> F[(S, A)].
F is the wrapping monad (Result, FutureResult, Maybe, etc.).
Composition uses duck-typed .bind() / .map() / .handle_error_with()
on whatever F returns — one implementation for all monads.
Haskell: StateT m s a
Scala: StateT[F[_], S, A]
Source code in funstruct/experimental/monadtransformer/state_t.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | |
run(initial_state)
Execute with initial state.
Returns F[(final_state, value)].
Source code in funstruct/experimental/monadtransformer/state_t.py
47 48 49 50 51 52 | |
bind(f)
FlatMap: thread state, pass value to f.
from funstruct.monad.option import Option, Some StateT.pure(1, Option).bind(lambda x: StateT.pure(x + 10, Option)).run(0) Some((0, 11))
Source code in funstruct/experimental/monadtransformer/state_t.py
54 55 56 57 58 59 60 61 62 63 64 65 | |
map(f)
Transform the produced value without touching state.
from funstruct.monad.option import Option, Some StateT.pure(5, Option).map(lambda x: x * 2).run(0) Some((0, 10))
Source code in funstruct/experimental/monadtransformer/state_t.py
67 68 69 70 71 72 73 74 75 76 77 78 | |
handle_error_with(f)
Recover from failure.
f receives the error, returns a recovery StateT.
Only works when F supports .handle_error_with().
Source code in funstruct/experimental/monadtransformer/state_t.py
80 81 82 83 84 85 86 87 88 89 90 | |
and_then(other)
Kleisli composition: value from self becomes initial state for other.
Short-circuits on inner monad failure.
Source code in funstruct/experimental/monadtransformer/state_t.py
92 93 94 95 96 97 | |
do(gen_fn)
classmethod
Do-notation via generators. Returns a callable.
Each yield extracts the value from a StateT.
State threads through, short-circuits on inner monad failure.
from funstruct.monad.either import Either, Right def pipeline(): ... x = yield StateT(lambda s: Right((s + 1, s))) ... y = yield StateT(lambda s: Right((s + 1, s))) ... return x + y StateT.do(pipeline)().run(0) Right((2, 1))
Source code in funstruct/experimental/monadtransformer/state_t.py
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | |
pure(value, monad)
classmethod
Lift a value into StateT. State unchanged.
from funstruct.monad.option import Option, Some StateT.pure("hello", Option).run(99) Some((99, 'hello'))
Source code in funstruct/experimental/monadtransformer/state_t.py
140 141 142 143 144 145 146 147 148 | |
fail(err, monad)
classmethod
Lift an error. Uses monad.raise_error.
Source code in funstruct/experimental/monadtransformer/state_t.py
150 151 152 153 | |
get(monad)
classmethod
Produce current state as the value.
from funstruct.monad.option import Option, Some StateT.get(Option).run(42) Some((42, 42))
Source code in funstruct/experimental/monadtransformer/state_t.py
155 156 157 158 159 160 161 162 163 | |
set(state, monad)
classmethod
Replace the state entirely, produce None.
Cats: StateT.set
from funstruct.monad.option import Option, Some StateT.set(99, Option).run(0) Some((99, None))
Source code in funstruct/experimental/monadtransformer/state_t.py
165 166 167 168 169 170 171 172 173 174 175 | |
inspect(f, monad)
classmethod
Get a function of the state as the value, without modifying state.
Cats: StateT.inspect
from funstruct.monad.option import Option, Some StateT.inspect(lambda s: s * 2, Option).run(5) Some((5, 10))
Source code in funstruct/experimental/monadtransformer/state_t.py
177 178 179 180 181 182 183 184 185 186 187 | |
modify(f, monad)
classmethod
Modify state, produce None.
from funstruct.monad.option import Option, Some StateT.modify(lambda s: s + 1, Option).run(5) Some((6, None))
Source code in funstruct/experimental/monadtransformer/state_t.py
189 190 191 192 193 194 195 196 197 | |
lift_f(inner)
classmethod
Lift F[A] into StateT — state unchanged.
Haskell equivalent: lift :: m a -> StateT s m a
from funstruct.monad.option import Option, Some, Nothing StateT.lift_f(Some(42)).run(0) Some((0, 42)) StateT.lift_f(Nothing()).run(0) Nothing()
Source code in funstruct/experimental/monadtransformer/state_t.py
199 200 201 202 203 204 205 206 207 208 209 210 211 | |
from_state(state, monad)
classmethod
Lift a pure State (S → (S, A)) into StateT.
Use when you have the inner state function but not the outer monad.
from funstruct.monad.option import Option, Some StateT.from_state(lambda s: (s + 1, s), Option).run(0) Some((1, 0))
Source code in funstruct/experimental/monadtransformer/state_t.py
213 214 215 216 217 218 219 220 221 222 223 | |