问题
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