Kleisli

class Kleisli<F, A, B>(val monad: Monad<F>, val run: (A) -> K1<F, B>) : K3<Kleisli.T, F, A, B> (source)

A monadic function (A) -> F<B> treated as an arrow, for some monad F.

Use it to compose effectful functions as first-class values: chain them with compose, adapt inputs/outputs with dimap, and route through pairs or Either with the arrow combinators.

Instances: Profunctor, Strong, Choice, Compose, Category, Arrow, ArrowChoice, ArrowApply (each requires the underlying Monad<F>), and ArrowLoop (requires MonadFix<F>).

val monad = object : IdentityMonad {}
val inc = Kleisli(monad) { a: Int -> Identity(a + 1) }
val double = Kleisli(monad) { a: Int -> Identity(a * 2) }
inc.compose(double).run(3) // Identity(8)
inc.first<String>().run(Pair(1, "x")) // Identity((2, x))
inc.dimap({ s: String -> s.length }, { "n=$it" }).run("ab") // Identity(n=3)

Constructors

Link copied to clipboard
constructor(monad: Monad<F>, run: (A) -> K1<F, B>)

Types

Link copied to clipboard
object Companion
Link copied to clipboard
class T

Witness type used to encode Kleisli as a higher-kinded K3<Kleisli.T, F, A, B>.

Properties

Link copied to clipboard
val monad: Monad<F>

the monad used to sequence effects.

Link copied to clipboard
val run: (A) -> K1<F, B>

the underlying function.

Functions

Link copied to clipboard
fun <C> combine(g: K3<Kleisli.T, F, A, C>): K3<Kleisli.T, F, A, Pair<B, C>>

Runs this arrow and g on the same input and pairs the results.

Link copied to clipboard
fun <C> compose(g: K3<Kleisli.T, F, B, C>): Kleisli<F, A, C>

Runs this arrow, then feeds its result into g (left-to-right composition).

Link copied to clipboard
fun <Z, C> dimap(f: (Z) -> A, g: (B) -> C): Kleisli<F, Z, C>

Pre-composes f on the input and post-maps g over the output.

Link copied to clipboard
fun <C> fanin(g: K3<Kleisli.T, F, C, B>): Kleisli<F, Either<A, C>, B>

Merges two arrows with the same output: this one for Either.Left, g for Either.Right.

Link copied to clipboard
fun <C> first(): Kleisli<F, Pair<A, C>, Pair<B, C>>

Applies this arrow to the first component of a pair, passing the second through.

Link copied to clipboard
fun <C> left(): Kleisli<F, Either<A, C>, Either<B, C>>

Applies this arrow to Either.Left inputs, passing Either.Right through.

Link copied to clipboard
infix fun <Z> lmap(f: (Z) -> A): Kleisli<F, Z, B>

Pre-composes f on the input.

Link copied to clipboard
fun <C> right(): Kleisli<F, Either<C, A>, Either<C, B>>

Applies this arrow to Either.Right inputs, passing Either.Left through.

Link copied to clipboard
infix fun <C> rmap(g: (B) -> C): Kleisli<F, A, C>

Maps g over the output.

Link copied to clipboard
fun <C> second(): K3<Kleisli.T, F, Pair<C, A>, Pair<C, B>>

Applies this arrow to the second component of a pair, passing the first through.

Link copied to clipboard
fun <C, D> split(g: K3<Kleisli.T, F, C, D>): K3<Kleisli.T, F, Pair<A, C>, Pair<B, D>>

Runs this arrow on the first component and g on the second.

Link copied to clipboard
fun <C, D> splitChoice(g: K3<Kleisli.T, F, C, D>): Kleisli<F, Either<A, C>, Either<B, D>>

Runs this arrow on Either.Left inputs and g on Either.Right inputs.