如何将List[zio.Task[List[A]]]转换为zio.任务[[List[A]]



我需要处理一组Id,并将结果作为zio返回。任务[List[RevantReadingRow]]

def getBaselinesForRequestIds(baseLineReqIds: Set[String]): Task[List[RelevantReadingRow]] =
dynamoConnection
.run(
table.getAll("baseline_req_id" in baseLineReqIds)
)
.flatMap(_.toList.separate match {
case (err :: _, _)           => ZIO.fail(new Throwable(describe(err)))
case (Nil, relevantReadings) => ZIO.succeed(relevantReadings)
})

上面的代码可以工作,但我需要将它们作为mutch以25个元素一批的方式处理。我试过这个,但后来我得到了一个zio列表。任务

def getBaselinesForRequestIds(baseLineReqIds: Set[String]): Task[List[RelevantReadingRow]] = {
val subSet = baseLineReqIds.grouped(25).toList
val res = for {
rows <- subSet.map(reqIds => dynamoConnection
.run(
table.getAll("baseline_req_id" in reqIds)
).flatMap(e => e.toList.separate match {
case (err :: _, _) => ZIO.fail(new Throwable(describe(err)))
case (Nil, relevantReadings) => ZIO.succeed(relevantReadings)
}))
} yield rows
res // this is List[zio.Task[List[RelevantReadingRow]]]
} 

我不知道如何转换回zio。任务[List[RevantReadingRow]]

有什么建议吗?

您可以使用ZIO.collectAll将List[Task]转换为Task[List],我以为是ZIO.sequence…,也许我与猫混淆了。。。

以下示例适用于Zio2


package sample
import zio._
object App extends ZIOAppDefault {
case class Result(value: Int) extends AnyVal
val data: List[Task[List[Result]]] = List(
Task { List(Result(1), Result(2)) },
Task { List(Result(3)) }
)
val flattenValues: Task[List[Result]] = for {
values <- ZIO.collectAll { data }
} yield values.flatten
val app = for {
values <- flattenValues
_      <- Console.putStrLn { values }
} yield()
def run = app
}

特别是你的样品。。。假设"分离"只是一种收集一些错误的扩展方法(返回错误列表和结果列表的元组(,而忽略"描述"方法将错误变成可丢弃的

https://scastie.scala-lang.org/jgoday/XKxVP2ECSFOv4chgSFCckg/7


package sample
import zio._

object App extends ZIOAppDefault {
class DynamoMock {
def run: Task[List[RelevantReadingRow]] = Task {
List(
RelevantReadingRow(1),
RelevantReadingRow(2),
)
}
}
case class RelevantReadingRow(value: Int) extends AnyVal
implicit class ListSeparate(list: List[RelevantReadingRow]) {
def separate: (List[String], List[RelevantReadingRow]) =
(Nil, list)
}
def getBaselinesForRequestIds(baseLineReqIds: Set[String]): Task[List[RelevantReadingRow]] = {
val dynamoConnection = new DynamoMock()
val subSet = baseLineReqIds.grouped(25).toList
val res: List[Task[List[RelevantReadingRow]]] = for {
rows <- subSet.map(reqIds => dynamoConnection
.run.flatMap(e => e.toList.separate match {
case (err :: _, _) => ZIO.fail(new Throwable(err))
case (Nil, relevantReadings) => ZIO.succeed(relevantReadings)
}))
} yield rows
for {
rows <- ZIO.collectAll(res)
} yield rows.flatten
}
val app = for {
values <- getBaselinesForRequestIds(Set("id1", "id2"))
_      <- Console.putStrLn { values }
} yield()
def run = app
}

因此,这里有一个基于@jgoday答案的替代解决方案

def getBaselinesForRequestIds(baseLineReqIds: Set[String]): Task[List[RelevantReadingRow]] =
for {
values <- ZIO.foreachPar(baseLineReqIds.grouped(25).toList) {
reqIds =>
dynamoConnection
.run(
table.getAll("baseline_req_id" in reqIds)
).flatMap(e => e.toList.separate match {
case (err :: _, _) => ZIO.fail(new Throwable(describe(err)))
case (Nil, relevantReadings) => ZIO.succeed(relevantReadings)
})
}
} yield values.flatten

相关内容

  • 没有找到相关文章

最新更新