MonadTrans

interface MonadTrans<F, T> : Monad<T> (source)

A monad transformer: a monad T built on top of a base monad F into which base computations can be lifted.

Kotlin's kind encoding cannot abstract over "a transformer applied to any base", so an instance fixes the base F and names the transformed monad T explicitly (e.g. MonadTrans<F, K1<MaybeT.T, F>>).

Laws:

  • lift(base.pure(a)) == pure(a)

  • lift(base.bind(f, m)) == bind({ a -> lift(f(a)) }, lift(m))

See also

Inheritors

Functions

Link copied to clipboard
abstract fun <A, B> ap(f: K1<T, (A) -> B>, v: K1<T, A>): K1<T, B>

Applies the function(s) in f to the value(s) in v, combining both effects.

Link copied to clipboard
open fun <A, B> bind(f: (A) -> K1<T, B>, v: K1<T, A>): K1<T, B>

Sequentially composes v with f, feeding each result of v to f (a.k.a. flatMap / >>=).

Link copied to clipboard
abstract fun <A, B> fmap(f: (A) -> B, v: K1<T, A>): K1<T, B>

Applies f to every value inside v.

Link copied to clipboard
fun <T, A> Monad<T>.fx(block: suspend MonadScope<T>.() -> A): K1<T, A>

Do-notation for monads that call the continuation passed to bind at most once per run. Each run of the resulting value starts a fresh coroutine, so lazy monads can be run repeatedly.

Link copied to clipboard
fun <T, A> Monad<T>.fxReplay(block: suspend MonadScope<T>.() -> A): K1<T, A>

Do-notation for any monad, including those that call the continuation passed to bind several times. The block is re-run from the start for every bound value, so code between binds may run multiple times.

Link copied to clipboard
open override fun <A, B> imap(f: (A) -> B, g: (B) -> A, v: K1<T, A>): K1<T, B>

Implements Invariant.imap by ignoring the backward function.

Link copied to clipboard
abstract fun <A> join(v: K1<T, K1<T, A>>): K1<T, A>

Flattens one level of nesting.

Link copied to clipboard
abstract fun <A> lift(v: K1<F, A>): K1<T, A>

Embeds a computation of the base monad into the transformed monad.

Link copied to clipboard
abstract fun <A> pure(v: A): K1<T, A>

Wraps v in the minimal context.