type Dataset = (DataSpecification, DataSetRef)
val clients = Nested[Task, Either[DataLakeRepositoryError, ?], Seq[Client]](dlRegistry.listClients())
def clientDatasets(client: String) =
Nested[Task, Either[DataLakeRepositoryError, ?], Seq[Dataset]](dlRegistry.listDatasets(client))
val r: Task[_] = (clients map { cls: Seq[Client] ⇒
cls map { client ⇒
(clientDatasets( client.id) map { dsds: Seq[Dataset] ⇒
dsds map { ds ⇒
ds._2.shortName.pp("\n\n"); ds
}
}).value
}
}).value
s"r is ${await(r)}".pp
r is Right(List(Task.Map$995060245, Task.Map$958851644, Task.Map$119297310, Task.Map$713902690, Task.Map$1792185060, Task.Map$1688986248))
Task.sequence
and other things but the result is always something else than desired.
cats.data.Nested
see https://typelevel.org/cats/datatypes/nested.html
def listClients(): Task[Either[DataLakeRepositoryError, Seq[Client]]]
def listDatasets(clientId: String): Task[Either[DataLakeRepositoryError, Seq[(DataSpecification, DataSetRef)]]]
Task.Suspend
and did not know what to do with them.
Task.Suspend
is internal, that's not what matters, but rather the type you're expecting.
F[List[Client]]
and String => F[List(DataSpec, DataRef)]
.flatMap
to get to List[Client]
traverse(c => listDataSets(c.id))
flatTraverse
Nested/EitherT
Stream/Observable
rather than List
, it would be easier still)
List
EitherT
here, not just Nested
, because you have some flatMap
s
object Ex {
import cats._, data._, implicits._
import cats.effect._
type Task[A] = IO[A]
type DataLakeRepositoryError
type DataSpecification
type DataSetRef
case class Client(id: String)
def listClients: Task[Either[DataLakeRepositoryError, List[Client]]] = ???
def listDatasets(clientId: String): Task[Either[DataLakeRepositoryError, List[(DataSpecification, DataSetRef)]]] = ???
def result: Task[Either[DataLakeRepositoryError, List[DataSetRef]]] =
EitherT(listClients).flatMap { clients =>
clients.flatTraverse(c => EitherT(listDatasets(c.id)).map(_.map(_._2)))
}.value
}
.map(_.map(_._2))
is a bit ugly, but it is what it is
IO
in this channel :P )