Scala 中的应用组合示例 [英] Example of Applicative composition in Scala
问题描述
这是我旧的问题的后续::>
我知道 monad 是不可组合的,即如果 M1[_]
和 M2[_]
是 monad M2[M1[_]]
不一定是 monad.例如,List[Int]
和 Option[Int]
是单子,但 Option[List[Int]]
不是自动 一个 monad,因此我需要一个 monad 转换器
将它用作一个 monad(如 此处)
我知道应用函子是可组合的.我想这意味着如果 A1[_]
和 A2[_]
是 applicatives 那么 A2[A1[_]]
是 总是 一个应用程序.这是正确的吗 ?
当 A1
是 List
并且 A2
是 Option
时,你能提供一个这样的组合的例子吗?可以举个例子吗?
我不久前在 scalaz 源代码中添加了一些相关的示例.我添加了使用组合 Apply 实例的示例(Apply 是 Applicative,没有点方法):
但是是的,对于我们有 Applicative[M1]
和 M2[_]
的任何 M1[_]
,我们有Applicative[M2]
,M1[M2[_]]
是applicative,你可以用Applicative[M1] compose Applicative[M2]代码>
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 ?
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):
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]
这篇关于Scala 中的应用组合示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!