Search code examples
scalaioscala-cats

cats-effect: How to transform `List[IO]` to `IO[List]`


I created a list of IO[Unit] in order to retrieve data from a list of URL. But now how I convert it back to a single IO[Unit] ?


Solution

  • You can do this in the following way

    val x: List[IO[Unit]] = ???
    
    import cats.implicits._
    
    val y: IO[List[Unit]] = x.sequence
    
    val z: IO[Unit] = y.map(_ => ())