Created
June 5, 2025 12:06
-
-
Save benhutchison/ef295940a19567360fc09b269a6d011e to your computer and use it in GitHub Desktop.
Self-contained example of Direct- vs Monadic- Syntax in Scala
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
//> using scala 3.7.0 | |
//> using dep org.typelevel::cats-effect:3.6.1 | |
//> using dep org.tpolecat::skunk-core:1.0.0-M10 | |
//> using dep io.github.dotty-cps-async::dotty-cps-async:1.0.2 | |
//> using dep io.github.dotty-cps-async::cps-async-connect-cats-effect:1.0.2 | |
import cats.* | |
import cats.implicits.* | |
import cats.effect.* | |
import cats.effect.implicits.* | |
import cats.effect.std.UUIDGen | |
import skunk.Session | |
import java.util.UUID | |
import cps.* | |
import cps.monads.catsEffect.given | |
case class Record(field: String, field2: Int) | |
def queryRecordServer[F[_]: Async](id: Long): F[(status: Int, r: Option[Record])] = ??? | |
def newUUID[F[_]: {Monad, UUIDGen}]: F[UUID] = ??? | |
def saveRecord[F[_]: {Monad, Session}](entity: (UUID, Record)): F[Unit] = ??? | |
def fetchStoreRecord[F[_]: {Async as A, UUIDGen, Session}](id: Long): F[Unit] = | |
for (status, optRecord) <- queryRecordServer(id) | |
_ <- if status == 200 && optRecord.isDefined then | |
for id <- newUUID | |
_ <- saveRecord(id -> optRecord.get) | |
yield () | |
else A.unit | |
yield () | |
def fetchStoreRecordDirect[F[_]: {Async, UUIDGen, Session}](id: Long): F[Unit] = async[F]: | |
val (status, optRecord) = queryRecordServer(id).await | |
if status == 200 && optRecord.isDefined then | |
saveRecord(newUUID.await -> optRecord.get).await | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment