Last active
March 14, 2018 12:22
-
-
Save voidcontext/5ae8c24bf5dc97821e870025a29b4fdc to your computer and use it in GitHub Desktop.
Nesting IO
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cats.effect.IO | |
import scala.concurrent.{Await, Future} | |
import scala.concurrent.duration._ | |
import scala.concurrent.ExecutionContext.Implicits.global | |
case class Repository() | |
case class MetaData(name: String) | |
trait APIClient { | |
def getRepositories: IO[List[Repository]] | |
def getMetadata(r: Repository): IO[MetaData] | |
} | |
def allMetaData(client: APIClient): IO[List[IO[MetaData]]] = | |
client.getRepositories.map( | |
_.map( | |
repository => | |
client.getMetadata(repository) | |
) | |
) | |
// Naive implementation for demostration purposes | |
def printMetaDataOrderedByName(allMetaData: IO[List[IO[MetaData]]]): Unit = | |
Await.result( | |
Future.sequence( | |
allMetaData.unsafeRunSync().map(_.unsafeToFuture()) | |
), | |
1.minute | |
).sortWith(_.name < _.name) | |
.foreach(println) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment