Benefits of Using Scalaz Kleisli

In scalaz, Kleisli[M[_], A, B] is a wrapper A => M[B] that allows you to create such functions. For example, if M[_] is a monad, I can compose Kleisli[M, A, B] and Kleisli[M, B, C] with >=> to get Kleisli[M, A, C] .

In a nutshell, Kleisli provides fantasy andThens depending on M Is it correct? Are there other benefits to using Kleisli ?

+6
source share
1 answer

Here are two benefits as examples - I'm sure you could come up with others.

Firstly, it can be useful to abstract from different arrows, such as Kleisli[M, ?, ?] And ? => ? ? => ? . For example, I can write a general function that will apply endomorphism a certain number of times.

 def applyX10[Arr[_, _]: Category, A](f: Arr[A, A]) = List.fill(10)(Endomorphic(f)).suml 

Now I can use this, for example. Int => Int or Kleisli[Option, Int, Int] :

 val f = (_: Int) + 1 val k = Kleisli.kleisli[Option, Int, Int] { case i if i % 2 == 0 => Some(i * 3) case _ => None } 

And then:

 scala> applyX10(f).run(1) res0: Int = 11 scala> applyX10[=?>, Int](k).run(2) res1: Option[Int] = Some(118098) 

(Note that A =?> B is just an alias for Kleisli[Option, A, B] .)

Secondly, the fact that Kleisli[F, ?, ?] Has a monad instance, if F can also be useful. See for example my answer here for a demonstration of how you can use a monadic composition with ReaderT , which is just an alias for Kleisli .

+9
source

Source: https://habr.com/ru/post/983789/


All Articles