我只是在尝试学习URIO,一切都很顺利,直到我得到了一个Seq[URIO[Any, Fiber.Runtime[Throwable, Unit]]] --我真的不确定该怎么做--我的纤维都没有运行,可能是因为我需要评估每个URIO,但如果是这样的话,我该如何在zio.App中做到这一点?我尝试过在序列上进行映射,但似乎总是以Seq[Nothing]结束……
我已经尝试过在效果中包装Seq,但这似乎对我没有帮助。我已经看了ZIO网站上的示例,但它们不处理纤维列表,所以现在我迷路了……
编辑
好的--我想我的问题是:如何运行效果列表?
package testmesomemore
import zio._
import zio.console._
import zio.internal.Platform
import java.io.IOException
case class testme(
s: String,
n: Int,
l: String) {
def runme() = UIO.effectTotal {
println(s"${s}${l}")
}
}
object mytest extends zio.App {
override val platform: Platform = zio.Runtime.default.platform.withReportFailure(_ => ())
def run(args: List[String]) = {
val testlist: Seq[testme] = List[testme](testme("Hi there", 1, "."), testme("This is a test", 3, "..."))
val result = for {
// r <- testlist.map(d => d.runme().forkDaemon) // How to run a list of effects?
a <- testme("Hi there", 1, ".").runme().forkDaemon
b <- testme("This is a test", 3, "...").runme().forkDaemon
} yield()
result.exitCode
}
}我可以单独派生效果,但是如果我有一个列表,我如何派生所有的效果呢?
我想我已经关注这个问题太久了,现在什么都说不通了……抱歉,如果我是愚蠢的,错过了显而易见的东西?
发布于 2021-09-14 16:39:50
除非您有管理纤程的特定需求,否则只需使用Zio.foreach或ZIO.foreachPar进行并行,如下所示
ZIO.foreachPar(testlist)(_.runme()).exitCodehttps://stackoverflow.com/questions/68655240
复制相似问题