scalaz.io:扁平化IO [Throwable,Seq [IO [Nothing,String]]]

时间:2018-12-22 09:08:26

标签: scala functional-programming scalaz

我有IO[Throwable, Seq[IO[Nothing, String]]],我想把它变成 IO[Throwable, Seq[String]]。任何想法如何?我尝试使用flatten,但收到奇怪的隐式丢失错误。

No implicits found for parameter asTraversable: IO[Nothing, Step] => GenTraversableOnce[B_]

谢谢!

1 个答案:

答案 0 :(得分:2)

我找到了解决方法:

val data1: IO[Throwable, Seq[IO[Nothing, String]]]
val data2: IO[Throwable, IO[Nothing, Seq[String]]] = data1.map(IO.sequence)
val data3: IO[Throwable, Seq[String]] = data2.flatten

要一步完成:

data1.flatMap(IO.sequence)

@andrey-tyukin

提供