kleisli

Scalaz Kleisli usage benefits

最后都变了- 提交于 2019-12-03 14:05:47
In scalaz Kleisli[M[_], A, B] is a wrapper of A => M[B] , which allows composition of such functions. For instance, if M[_] is 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 fancy andThens depending on M . Is it correct ? Are there other benefits of using Kleisli ? Travis Brown Here are two benefits as examples—I'm sure you could come up with others. First, it can be useful to abstract over different arrows, such as Kleisli[M, ?, ?] and ? => ? . For example, I can write a generic function that will apply an

Is it just a coincidence that Kleisli, ReaderT, and Reader are the same in Scalaz

我只是一个虾纸丫 提交于 2019-12-03 01:54:02
In Scalaz Kleisli[F, A, B] is a wrapper for A => F[B] . ReaderT[F, A, B] -- reader monad transformer -- is just an alias of Kleisli[F, A, B] . Reader[A, B] monad is a specialization of ReaderT with identity monad Id : type Reader[A, B] = ReaderT[Id, A, B] . Is it just a coincidence or there are some deeper reasons that Kleisli , ReaderT , and Reader are isomorphic in Scalaz ? You can think of it as arriving at the same place by two different routes. On one side you start with the reader monad, which is simply a kind of wrapper for functions. Then you realize that you want to integrate this

How to fix this exercise with Endomorphic wrapper?

走远了吗. 提交于 2019-12-01 23:56:56
This is a follow-up to my previous question . Suppose I need to find an XML node by path. I can write a function to get a child node by name import scala.xml.{Node => XmlNode} def child(name: String): XmlNode = Option[XmlNode] = _.child.find(_.label == name) I compose the child functions with kleisli; e.g. scala> val a = <a><a0><a1><a2/></a1></a0></a> a: scala.xml.Elem = <a><a0><a1><a2/></a1></a0></a> scala> val find01 = Kleisli(child("a0")) >=> Kleisli(child("a1")) findAB: scalaz.Kleisli[Option,scala.xml.Node,scala.xml.Node] = Kleisli(<function1>) scala> find01(a) res85: Option[scala.xml.Node