简体   繁体   English

Scalaz7中的`sequence`在哪里?

[英]Where is `sequence` in Scalaz7

I am learning Scalaz and I have a project that already makes use of Scalaz7. 我正在学习Scalaz,我有一个已经使用Scalaz7的项目。 Following this question I would like to use the function 关注这个问题我想使用这个功能

sequence[T](l: List[Option[T]]): Option[List[T]]

(not that it is hard to write it myself). (并不是说我自己写的很难)。 But the aforementioned question mentions Scalaz6. 但上述问题提到了Scalaz6。

Where to find the sequence function in Scalaz7? 在Scalaz7中哪里可以找到序列函数?

It's defined in the scalaz.Traverse type class, where it looks like this: 它在scalaz.Traverse类型类中定义,它看起来像这样:

def sequence[G[_]:Applicative,A](fga: F[G[A]]): G[F[A]] =
  traversal[G].run[G[A], A](fga)(ga => ga)

scalaz.syntax.TraverseOps provides a version that gets pimped onto List , since List has a Traverse instance. scalaz.syntax.TraverseOps提供了一个获取scalaz.syntax.TraverseOpsList的版本,因为List有一个Traverse实例。

You can either import just what you need: 你可以输入你需要的东西:

import scalaz._, std.list._, std.option._, syntax.traverse._

Or everything and the kitchen sink: 或者一切和厨房水槽:

import scalaz._, Scalaz._

And then you can use it like this: 然后你可以像这样使用它:

scala> val xs: List[Option[Int]] = Some(1) :: Some(2) :: Nil
xs: List[Option[Int]] = List(Some(1), Some(2))

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

Or if you want exactly the formulation in your question: 或者,如果您想要在您的问题中完全符合要求:

scala> def sequence[T](l: List[Option[T]]): Option[List[T]] = l.sequence
sequence: [T](l: List[Option[T]])Option[List[T]]

scala> sequence(xs)
res1: Option[List[Int]] = Some(List(1, 2))

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

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