简体   繁体   English

Scala中的适用成分示例

[英]Example of Applicative composition in Scala

This is a followup to my old questions : 这是我的老问题的跟进:

I know that monads are not composable, ie if M1[_] and M2[_] are monads M2[M1[_]] is not necessarily a monad. 我知道单子不是可组合的,即如果M1[_]M2[_]是单子,则M2[M1[_]]不一定是单子。 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 ) 例如, List[Int]Option[Int]是monad,但是Option[List[Int]]并不是自动的monad,因此我需要monad transformer才能将其用作monad(如此

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. 我想这意味着如果A1[_]A2[_]是可应用的,则A2[A1[_]] 始终是可应用的。 Is it correct ? 这是对的吗 ?

Could you provide an example of such a composition when A1 is List and A2 is Option ? A1ListA2Option时,您能否提供这种组合的示例? Could you give an example of other applicatives composed ? 您能举一个由其他应用组成的示例吗?

I added some examples to the scalaz source a while back which are relevant. 我不久前在scalaz源中添加了一些相关的示例。 I added examples of using composed Apply instances (Apply is Applicative without the point method): 我添加了使用组合的Apply实例的示例(Apply是Applicative,没有point方法):

https://github.com/scalaz/scalaz/blob/series/7.2.x/example/src/main/scala/scalaz/example/ApplyUsage.scala#L132-L147 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] 但是,对于任何M1[_]我们掌握Applicative[M1]M2[_]我们掌握Applicative[M2] M1[M2[_]]是应用性的,你可以得到应用型实例Applicative[M1] compose Applicative[M2]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM