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)Content copied to clipboard