OptionT
funstruct.experimental.monadtransformer.option_t
OptionT — adds optionality to any monad F.
OptionT[F, A] wraps F[Option[A]]. bind short-circuits on Nothing, map transforms the value inside Some.
Examples:
>>> from funstruct.experimental.monadtransformer.option_t import OptionT
>>> from funstruct.monad.either import Either, Right, Left
>>> from funstruct.monad.option import Some, Nothing
Wrapping Either — a computation that can fail OR be absent:
>>> OptionT(Right(Some(1))).map(lambda x: x + 10).run()
Right(Some(11))
>>> OptionT(Right(Nothing())).map(lambda x: x + 10).run()
Right(Nothing())
>>> OptionT(Left("db error")).map(lambda x: x + 10).run()
Left('db error')
bind chains — short-circuits on Nothing OR Left:
>>> OptionT(Right(Some(1))).bind(
... lambda x: OptionT(Right(Some(x * 10)))
... ).run()
Right(Some(10))
>>> OptionT(Right(Nothing())).bind(
... lambda x: OptionT(Right(Some(x * 10)))
... ).run()
Right(Nothing())
handle_error_with — recover from Nothing (not from Left):
>>> OptionT(Right(Nothing())).handle_error_with(
... lambda: OptionT(Right(Some(99)))
... ).run()
Right(Some(99))
lift_f — bring F[A] into OptionT (wraps in Some):
>>> OptionT.lift_f(Right(42)).run()
Right(Some(42))
OptionT
Bases: MonadTransformer, Generic[_F, _A]
OptionT: F[Option[A]].
Adds optionality/absence to any monad F. bind delegates to F's bind, then pattern-matches the Option inside: Some(v) continues, Nothing short-circuits.
Haskell: MaybeT m a
Scala: OptionT[F[_], A]
Source code in funstruct/experimental/monadtransformer/option_t.py
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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | |
run()
Unwrap to get F[Option[A]].
Source code in funstruct/experimental/monadtransformer/option_t.py
71 72 73 | |
bind(f)
FlatMap: unwrap F, match Option, chain on Some.
from funstruct.monad.either import Right from funstruct.monad.option import Some, Nothing OptionT(Right(Some(1))).bind( ... lambda x: OptionT(Right(Some(x + 10))) ... ).run() Right(Some(11)) OptionT(Right(Nothing())).bind( ... lambda x: OptionT(Right(Some(x + 10))) ... ).run() Right(Nothing())
Source code in funstruct/experimental/monadtransformer/option_t.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | |
map(f)
Transform the value inside Some inside F.
from funstruct.monad.either import Right from funstruct.monad.option import Some OptionT(Right(Some(5))).map(lambda x: x * 2).run() Right(Some(10))
Source code in funstruct/experimental/monadtransformer/option_t.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | |
handle_error_with(f)
Recover from Nothing: if inner is Nothing, use fallback.
from funstruct.monad.either import Right from funstruct.monad.option import Some, Nothing OptionT(Right(Nothing())).handle_error_with( ... lambda: OptionT(Right(Some(99))) ... ).run() Right(Some(99)) OptionT(Right(Some(1))).handle_error_with( ... lambda: OptionT(Right(Some(99))) ... ).run() Right(Some(1))
Source code in funstruct/experimental/monadtransformer/option_t.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | |
fold(on_nothing, on_some)
Eliminate the Option inside F, returning F[B].
from funstruct.monad.either import Right from funstruct.monad.option import Some, Nothing OptionT(Right(Some(5))).fold(lambda: 0, lambda x: x * 2) Right(10) OptionT(Right(Nothing())).fold(lambda: 0, lambda x: x * 2) Right(0)
Source code in funstruct/experimental/monadtransformer/option_t.py
141 142 143 144 145 146 147 148 149 150 151 | |
get_or_else(default)
Extract the Some value or return default, inside F.
from funstruct.monad.either import Right from funstruct.monad.option import Some, Nothing OptionT(Right(Some(42))).get_or_else(0) Right(42) OptionT(Right(Nothing())).get_or_else(0) Right(0)
Source code in funstruct/experimental/monadtransformer/option_t.py
153 154 155 156 157 158 159 160 161 162 163 | |
filter(f)
Filter the value inside Some; becomes Nothing if predicate fails.
from funstruct.monad.either import Right from funstruct.monad.option import Some, Nothing OptionT(Right(Some(10))).filter(lambda x: x > 5).run() Right(Some(10)) OptionT(Right(Some(3))).filter(lambda x: x > 5).run() Right(Nothing())
Source code in funstruct/experimental/monadtransformer/option_t.py
165 166 167 168 169 170 171 172 173 174 175 | |
and_then(other)
Kleisli composition: value from self becomes input for other's run.
Source code in funstruct/experimental/monadtransformer/option_t.py
177 178 179 | |
do(gen_fn)
classmethod
Do-notation via generators. Short-circuits on Nothing. Returns a callable.
from funstruct.monad.either import Right from funstruct.monad.option import Some def pipeline(): ... x = yield OptionT(Right(Some(1))) ... y = yield OptionT(Right(Some(x + 10))) ... return x + y OptionT.do(pipeline)().run() Right(Some(12))
Source code in funstruct/experimental/monadtransformer/option_t.py
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 | |
pure(value, monad)
classmethod
Lift a plain value into OptionT.
from funstruct.monad.either import Either, Right from funstruct.monad.option import Some OptionT.pure(42, Either).run() Right(Some(42))
Source code in funstruct/experimental/monadtransformer/option_t.py
220 221 222 223 224 225 226 227 228 229 | |
none(monad)
classmethod
Construct an OptionT holding Nothing.
from funstruct.monad.either import Either, Right from funstruct.monad.option import Nothing OptionT.none(Either).run() Right(Nothing())
Source code in funstruct/experimental/monadtransformer/option_t.py
231 232 233 234 235 236 237 238 239 240 | |
lift_f(fa)
classmethod
Lift F[A] into OptionT — wraps value in Some.
Use when you have the outer monad but not the inner Option.
Haskell equivalent: lift :: m a -> OptionT m a
from funstruct.monad.either import Right, Left from funstruct.monad.option import Some OptionT.lift_f(Right(42)).run() Right(Some(42)) OptionT.lift_f(Left("err")).run() Left('err')
Source code in funstruct/experimental/monadtransformer/option_t.py
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | |
from_option(option, monad)
classmethod
Lift Option[A] into OptionT — wraps in the outer monad.
Use when you have the inner Option but not the outer monad.
from funstruct.monad.either import Either, Right from funstruct.monad.option import Some, Nothing OptionT.from_option(Some(42), Either).run() Right(Some(42)) OptionT.from_option(Nothing(), Either).run() Right(Nothing())
Source code in funstruct/experimental/monadtransformer/option_t.py
259 260 261 262 263 264 265 266 267 268 269 270 271 272 | |