Home > database >  How to convert List[zio.Task[List[A]]] to zio.Task[[List[A]]
How to convert List[zio.Task[List[A]]] to zio.Task[[List[A]]

Time:03-17

I need to process a set of Ids and return the result as zio.Task[List[RelevantReadingRow]]

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)
    })

The code above works but I need to process them in batches of 25 element as mutch. I have try this but then I get a List of zio.Task

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]]]
  } 

I dont know how to convert back to a zio.Task[List[RelevantReadingRow]]

any sugestion?

CodePudding user response:

You can use ZIO.collectAll to convert List[Task] to Task[List], I thought it was ZIO.sequence..., maybe I'm getting confused with cats...

Following example works with 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
}

In particular for your sample ... and assuming that 'separate' it's just an extension method to collect some errors (returns a tuple of error list and result list), and ignoring the method 'describe' to turn an err into a throwable

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
}

CodePudding user response:

So, here is an alternative solution based on @jgoday answer

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
  • Related