Doobie transact over a list of ConnectionIO programs

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Doobie transact over a list of ConnectionIO programs



Let's say I have a list of Doobie programs (all with Unit type parameters, fwiw):


Unit


val progList: List[ConnectionIO[Unit]] = prog1 :: prog2 :: ... :: Nil



Is there any way I can run them in one transaction? A for-comprehension won't work here, because I only know the precise composition of the program list at runtime. Essentially, I suppose I need to fold them together, I guess.



I suppose this question applies to Free Monads in Cats in general, so I'll tag Cats as well. Thanks.





You want to traverse List[ConnectionIO[Unit]] into ConnectionIO[List[Unit]]? If so, that won't work: github.com/tpolecat/doobie/issues/465
– Yuval Itzchakov
2 hours ago




List[ConnectionIO[Unit]]


ConnectionIO[List[Unit]]





@YuvalItzchakov Isn't that ticket about the reverse operation?
– Alvaro Carrasco
9 secs ago




1 Answer
1



You can do that with .sequence from cats:


.sequence


import doobie.implicits._
import cats.implicits

...

val res = progList.sequence // ConnectionIO[List[Unit]]






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived