2.8 集合的新增内容。这个签名会是什么样子?类似于 scalaz 序列
我今天发现了一篇 博客文章,其中提到了 scalaz 的 序列函数。
难道你不能做一些简单的事情:
if (l contains None) None else l
如果是这样,这个函数签名会是什么样子? contains 是在 SeqLike 中,对吗?
另外,从博客文章中我认为序列将类似于映射,但一旦遇到 None 就会中断。有这样的事吗?
I found a blog post today that mention's scalaz's sequence function.
Couldn't you do something as simple as:
if (l contains None) None else l
If so, what would this function signature look like? contains is in SeqLike, right?
Also, from the blog post I thought sequence was going to be something similar to map, but one that would break once None is encountered. Is there something like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,你可以,但应该是:
博客文章中的代码尝试尽可能通用地编写该函数(使用 scalaz 的抽象),因此它不仅适用于 Seq 中的选项。
[编辑]更正
Yes, you could, but it should be:
The code in the blog post tries to write that function as general as possible (using scalaz' abstractions), so it will work not only for Options in a Seq.
[Edit] Corrected
是的,您绝对可以编写专门针对某些特定数据结构的序列函数。然而,Scalaz 版本尽可能通用。因此,它适用于
F
和G
的任意组合,其中F[G[A]] => G[F[A]]
是可能的。您正在寻找的另一个函数称为
traverse
。它的签名x.traverse(f)
相当于x.map(f).sequence
。x.sequence
等价于x.traverse(a => a)
Yes you can definitely write the sequence function specialized to some specific data structure. The Scalaz version, however is as general as possible. So it will work for any combination of
F
andG
for whichF[G[A]] => G[F[A]]
is possible.The other function you're looking for is called
traverse
. It has the signaturex.traverse(f)
is equivalent tox.map(f).sequence
.x.sequence
is equivalent tox.traverse(a => a)