我的Scala中有以下内容
import scalaz.effect.IO
val i: Iterator[IO[List[String]]] = null
val ii: Iterator[IO[List[String]]] = for{ //This does not compile
io <- i;
lst <- io
} yield lst为什么?怎么了?
我以为ii和i完全一样。但它拒绝汇编:
Error:(12, 11) type mismatch;
found : scalaz.effect.IO[List[String]]
required: scala.collection.GenTraversableOnce[scalaz.effect.IO[List[String]]]
lst <- io发布于 2017-11-22 16:38:34
flatMap in scala
回想一下,for-理解只是对flatMap的甜蜜调用。
for {
a <- expr //expr must return M[A] such that M has a
//flatMap method: flatMap[B](f: A => N[B]) for some N
b <- f(a) //f(a) must be an N[B]
...你的问题
这是Iterator.flatMap的签名
def flatMap[B](f: A => GenTraversableOnce[B]): Iterator[B]但是您试图提供一个返回IO[B]的函数。
lst <- io //You cannot just have `io` on the right here因此产生了编译错误。
flatMap in scala again
Scala应用于集合类型(和选项)的flatMap <~> for-comprehension转换(在我看来)是令人困惑的,因为它们允许您在不同类型的Monad之间进行切换(例如列表/选项/Set等)。例如,这里的x的类型是什么?
val x =
for {
i <- List(1, 2, 3)
j <- Option(i + 1)
k <- Stream(i, j)
} yield k黄曲霉属()单色植物
仔细看看scalaz.Monad的flatMap
trait Monad[M[_]] {
def flatMap[A, B](ma: M[A])(f: A => M[B]): M[B]
}M的类型总是固定的。也就是说,在这种理解中:
lazy val ma: M[A] = ???
for (a <- ma; b <- f(a)) yield b对于某些f,函数M[B]的结果类型必须在M[B]中。虽然有时这可能有点烦人,但它的优点是成为完全可预测的。你从不为你的理解而感到困惑。
回到问题
你想要什么并不明显,但以下是一些建议:
i.toStream.sequence.map(flatten) //IO[Stream[String]]发布于 2017-11-22 16:12:44
i和io必须是同一个单子:
io <- i // i is an Iterator[...]
lst <- io // io is an IO[List[String]]发布于 2021-11-22 22:00:42
当使用IO和-理解时,一种选择是在您的理解中使用.unsafeToFuture .在您的示例中,这将是:
val ii: Iterator[IO[List[String]]] = for{
io <- i;
lst <- io.unsafeToFuture()
} yield lsthttps://stackoverflow.com/questions/47439124
复制相似问题