I have some code structurally identical to this and I'm not sure the best way to clean it up? The trivial IOs and additions are there so that the example compiles without needing additional methods.
I'd really like to not have it be so nested, is there a way to have a single for comprehension that supports both IO and List? I know theres OptionT for the option variant of this problem, but there doesn't seem to be an equivalent ListT.
Any advice would be greatly appreciated
import cats.Traverse
import cats.effect.IO
import cats.implicits._
def exampleDoingSomeThings: IO[Unit] = for {
ids <- IO.pure(List(1, 2, 3))
_ <- ids.traverse[IO, Unit](id => for {
users <- IO.pure(List(id + 4, id + 5, id + 6))
data <- IO(id + 7)
otherData <- IO(id + 8)
_ <- users.traverse[IO, Unit](ud => for {
innerData <- IO(id + ud)
innerState <- IO(ud + 9)
_ <- if (innerState > 15) for {
_ <- IO(println(s"action1: $id $ud"))
_ <- IO(println(s"action2: $id $ud"))
} yield () else IO.pure()
} yield ())
} yield ())
} yield ()
exampleDoingSomeThings.unsafeRunSync