Scala 中的应用组合示例 [英] Example of Applicative composition in Scala

查看:43
本文介绍了Scala 中的应用组合示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我旧的问题的后续::>

我知道 monad 是不可组合的,即如果 M1[_]M2[_] 是 monad M2[M1[_]] 不一定是 monad.例如,List[Int]Option[Int] 是单子,但 Option[List[Int]] 不是自动 一个 monad,因此我需要一个 monad 转换器 将它用作一个 monad(如 此处)

我知道应用函子是可组合的.我想这意味着如果 A1[_]A2[_] 是 applicatives 那么 A2[A1[_]]总是 一个应用程序.这是正确的吗 ?

A1List 并且 A2Option 时,你能提供一个这样的组合的例子吗?可以举个例子吗?

解决方案

我不久前在 scalaz 源代码中添加了一些相关的示例.我添加了使用组合 Apply 实例的示例(Apply 是 Applicative,没有点方法):

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

但是是的,对于我们有 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):

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]

这篇关于Scala 中的应用组合示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆