Example of Applicative composition in Scala

自闭症网瘾萝莉.ら 提交于 2019-12-12 12:33:05

问题


This is a followup to my old questions:

I know that monads are not composable, i.e. if M1[_] and M2[_] are monads M2[M1[_]] is not necessarily a monad. For instance, List[Int] and Option[Int] are monads but Option[List[Int]] is not automatically a monad and therefore I need a monad transformer to use it as a monad (as in here)

I know that applicative functors are composable. I guess it means that if A1[_] and A2[_] are applicatives then A2[A1[_]] is always an applicative. Is it correct ?

Could you provide an example of such a composition when A1 is List and A2 is Option ? Could you give an example of other applicatives composed ?


回答1:


I added some examples to the scalaz source a while back which are relevant. I added examples of using composed Apply instances (Apply is Applicative without the point method):

https://github.com/scalaz/scalaz/blob/series/7.2.x/example/src/main/scala/scalaz/example/ApplyUsage.scala#L132-L147

but yes, for any M1[_] for which we have Applicative[M1] and M2[_] for which we have Applicative[M2], M1[M2[_]] is applicative, and you can get the Applicative instance with Applicative[M1] compose Applicative[M2]



来源:https://stackoverflow.com/questions/28850636/example-of-applicative-composition-in-scala

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!