如果 A 和 B 是单子,如何将 A[B[C]] 转换为 B[A[C]]?

编程入门 行业动态 更新时间:2024-10-28 04:26:52
本文介绍了如果 A 和 B 是单子,如何将 A[B[C]] 转换为 B[A[C]]?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在寻找一个更通用的解决方案,它利用 monads(可能还有 monoids)来实现与if( xs.contains(None) ) None else Some(xs.flatten) 为 Seq[Option[A]] 类型的 xs 做.

I'm looking for a more general solution which exploits monads (and monoids possibly) to achieve the same as if( xs.contains(None) ) None else Some(xs.flatten) does for xs of type Seq[Option[A]].

如何使用 Scalaz 做到这一点?我觉得我遗漏了一些明显的东西.

How can I do that with Scalaz? I feel like I'm missing something evident.

推荐答案

有两个 monad 是不够的(对于 M)和已经足够了(对于 N)——当然这还不够——但是如果 M 有一个 Traverse 实例并且 N 有一个 Applicative> 例如,您可以使用 sequence.例如:

Having two monads is both not enough (for M) and more than enough (for N)—which adds up to not enough, of course—but if M has a Traverse instance and N has an Applicative instance, you can use sequence. For example:

import scalaz._, Scalaz._ def foo[A](xs: List[Option[A]]): Option[List[A]] = xs.sequence

这具有您想要的语义.请注意,我使用的是 List 而不是 Seq,因为 Scalaz 7 不再为 Seq 提供必要的 Traverse 实例>(尽管您可以轻松编写自己的).

This has the semantics you want. Note that I'm using List instead of Seq, since Scalaz 7 no longer provides the necessary Traverse instance for Seq (although you could easily write your own).

如您所见,以下内容无法编译:

As you've noticed, the following won't compile:

List(Some(1), Some(45)).sequence

虽然如果你把 None 扔进去也没关系:

Although it's fine if you throw a None in there:

scala> List(Some(1), None, Some(45)).sequence res0: Option[List[Int]] = None

这是因为 List(Some(1), Some(45)) 的推断类型将是 List[Some[Int]],而我们不Some 有一个 Applicative 实例.

This is because the inferred type of List(Some(1), Some(45)) will be List[Some[Int]], and we don't have an Applicative instance for Some.

Scalaz 提供了一个方便的 some 方法,它的工作方式与 Some.apply 类似,但为您提供了一些已经输入为 Option 的内容,因此您可以写如下:

Scalaz provides a handy some method that works like Some.apply but gives you something that's already typed as an Option, so you can write the following:

scala> List(some(1), some(45)).sequence res1: Option[List[Int]] = Some(List(1, 45))

无需额外输入.

更多推荐

如果 A 和 B 是单子,如何将 A[B[C]] 转换为 B[A[C]]?

本文发布于:2023-11-25 05:03:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1628440.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单子   转换为   如何将

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!