lisa.monad.Async#

class lisa.monad.Async(coro)[source]#

Bases: MonadTrans

Monad transformer allowing the decorated coroutine function to await on non-monadic values. This is useful to mix any monad transformer defined in this module with other async APIs, such as asyncio.

Attributes

Properties

coro

Coroutine that will only yield non-monadic values. All the monadic values will be processed by the monad transformer stack as expected and will stay hidden.

x

Run the coroutine to completion in its event loop.

Methods

bind()

Takes a monadic value Monad[A], a function that takes an A and returns Monad[B], and returns a Monad[B].

hoist()

Lift a monadic value m by one level in the stack, i.e.: Given a stack for 3 transformers T1(T2(T3(Identity))), a value m = T2(Identity).pure(42). we have T2.hoist(m, T3.pure) == T2(T3(Identity)).pure(42).

lift()

Lift a monadic value m by one level in the stack, i.e.: Given a stack for 3 transformers T1(T2(T3(Identity))), a value m = T3(Identity).pure(42). we have T2.lift(m) == T2(T3(Identity)).pure(42).

__await__() inherited

do() inherited

Decorate a coroutine function so that awaits gains the powers of the monad.

join() inherited

Takes a monadic value Monad[Monad[A]], and returns a Monad[A].

map() inherited

Takes a monadic value Monad[A], a function that takes an A and returns B, and returns a Monad[B].

pure() inherited

Turn a regular value of type A into a monadic value of type Monad[A].

Attributes#

Async.__slots__ = ('_coro',)#

Properties#

property Async.coro[source]#

Coroutine that will only yield non-monadic values. All the monadic values will be processed by the monad transformer stack as expected and will stay hidden.

property Async.x[source]#

Run the coroutine to completion in its event loop.

Methods#

Async.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.

Async.hoist(self, nat)[source]#

Lift a monadic value m by one level in the stack, i.e.: Given a stack for 3 transformers T1(T2(T3(Identity))), a value m = T2(Identity).pure(42). we have T2.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 to map, except that instead of traversing all the nested functor layers, it stops at the first one.

Parameters:

See also

hoist as defined in https://hackage.haskell.org/package/mmorph

Note

Note for implementers: A monad transformers t m a (t is the transformer HKT, m is the base monad and a 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 as data MaybeT m a = MaybeT (m (Maybe a)). What the hoist implementation must do is to “rebuild” a value with a call to nat() around the m (...) part. For MaybeT, this gives hoist nat (MaybeT (m (Maybe a))) = MaybeT(nat(m (Maybe a))).

classmethod Async.lift(x)[source]#

Lift a monadic value m by one level in the stack, i.e.: Given a stack for 3 transformers T1(T2(T3(Identity))), a value m = T3(Identity).pure(42). we have T2.lift(m) == T2(T3(Identity)).pure(42).

Async.__await__()#

Inherited method, see lisa.monad.Monad.__await__()

classmethod Async.do(f)#

Inherited method, see lisa.monad.MonadTrans.do()

Decorate a coroutine function so that awaits gains the powers of the monad.

classmethod Async.join(self)#

Inherited method, see lisa.monad.Monad.join()

Takes a monadic value Monad[Monad[A]], and returns a Monad[A].

classmethod Async.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 Async.pure(x)#

Inherited method, see lisa.monad.MonadTrans.pure()

Turn a regular value of type A into a monadic value of type Monad[A].