lisa.monad.Option#
- class lisa.monad.Option(x)[source]#
Bases:
MonadTrans
Monad transformer that manipulates
Some
andNothing
.Option.bind()
will short-circuit ifNothing
is passed, much like the Rust or Javascript?
operator, or theMaybeT
monad transformer in Haskell.Attributes
Properties
Wrapped value, of type
Base[_Optional[A]]
withBase
the base monad of the transformer.Methods
Takes a monadic value Monad[A], a function that takes an A and returns Monad[B], and returns a Monad[B].
Lift a monadic value
m
by one level in the stack, i.e.: Given a stack for 3 transformersT1(T2(T3(Identity)))
, a valuem = T2(Identity).pure(42)
. we haveT2.hoist(m, T3.pure) == T2(T3(Identity)).pure(42)
.Lift a monadic value
m
by one level in the stack, i.e.: Given a stack for 3 transformersT1(T2(T3(Identity)))
, a valuem = T3(Identity).pure(42)
. we haveT2.lift(m) == T2(T3(Identity)).pure(42)
.__await__()
inheriteddo()
inheritedDecorate a coroutine function so that
awaits
gains the powers of the monad.join()
inheritedTakes a monadic value Monad[Monad[A]], and returns a Monad[A].
map()
inheritedTakes a monadic value Monad[A], a function that takes an A and returns B, and returns a Monad[B].
pure()
inheritedTurn a regular value of type
A
into a monadic value of typeMonad[A]
.
Attributes#
- Option.__slots__ = ('_x',)#
Properties#
Methods#
- Option.bind(continuation)[source]#
Takes a monadic value Monad[A], a function that takes an A and returns Monad[B], and returns a Monad[B].
Note
It is allowed to return a
_TailCall
instance.
- Option.hoist(self, nat)[source]#
Lift a monadic value
m
by one level in the stack, i.e.: Given a stack for 3 transformersT1(T2(T3(Identity)))
, a valuem = T2(Identity).pure(42)
. we haveT2.hoist(m, T3.pure) == T2(T3(Identity)).pure(42)
.In other words, it allows adding a level “from below”, whereas
lift
adds a level “from above”. It’s similar tomap
, except that instead of traversing all the nested functor layers, it stops at the first one.- Parameters:
self (lisa.monad.Monad) – Monadic value to hoist.
nat (collections.abc.Callable) – Natural transform. i.e. a morphism from
Monad1[A]
toMonad2[A]
that obeys certain laws.
See also
hoist
as defined in https://hackage.haskell.org/package/mmorphNote
Note for implementers: A monad transformers
t m a
(t
is the transformer HKT,m
is the base monad anda
is the “contained type) usually ends up containing an “m (f a)” (f
being some kind of functor). For example,MaybeT
in Haskell (Option
here) is more or less defined asdata MaybeT m a = MaybeT (m (Maybe a))
. What thehoist
implementation must do is to “rebuild” a value with a call tonat()
around them (...)
part. ForMaybeT
, this giveshoist nat (MaybeT (m (Maybe a))) = MaybeT(nat(m (Maybe a)))
.
- classmethod Option.lift(m)[source]#
Lift a monadic value
m
by one level in the stack, i.e.: Given a stack for 3 transformersT1(T2(T3(Identity)))
, a valuem = T3(Identity).pure(42)
. we haveT2.lift(m) == T2(T3(Identity)).pure(42)
.See also
lift
as defined in https://hackage.haskell.org/package/transformers
- Option.__await__()#
Inherited method, see
lisa.monad.Monad.__await__()
- classmethod Option.do(f)#
Inherited method, see
lisa.monad.MonadTrans.do()
Decorate a coroutine function so that
awaits
gains the powers of the monad.
- classmethod Option.join(self)#
Inherited method, see
lisa.monad.Monad.join()
Takes a monadic value Monad[Monad[A]], and returns a Monad[A].
- classmethod Option.map(self, f)#
Inherited method, see
lisa.monad.Monad.map()
Takes a monadic value Monad[A], a function that takes an A and returns B, and returns a Monad[B].
- classmethod Option.pure(x)#
Inherited method, see
lisa.monad.MonadTrans.pure()
Turn a regular value of type
A
into a monadic value of typeMonad[A]
.