Created
October 25, 2018 14:07
-
-
Save julien-truffaut/a93335b7cbf11798c1d4a0ccbc6846d9 to your computer and use it in GitHub Desktop.
Mtl Example
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.Monad | |
import cats.data.EitherT | |
import cats.implicits._ | |
import cats.mtl.implicits._ | |
import cats.mtl.{ApplicativeAsk, FunctorRaise} | |
import doobie.free.connection.ConnectionIO | |
object MtlExample { | |
case class RequestId(value: String) | |
case class PersonId(value: String) | |
case class Person(id: PersonId, name: String, siblings: List[PersonId]) | |
sealed trait PersonError | |
case class PersonNotFound(id: Int) extends PersonError | |
case class InvalidName(name: String) extends PersonError | |
def getPerson(id: PersonId): EitherT[ConnectionIO, PersonError, Person] = ??? | |
// how to impl in using getPerson ? | |
def getPersonMtl[F[_]](id: PersonId)(implicit F: FunctorRaise[F, PersonError]): F[Person] = ??? | |
def log[F[_]](message: String)(implicit F: ApplicativeAsk[F, RequestId]): F[Unit] = | |
F.reader(id => println(s"$id $message")) | |
// how to run F in one transaction ? | |
def getSiblings[F[_]: Monad: ApplicativeAsk[?[_], RequestId] : FunctorRaise[?[_], PersonError]](id: PersonId): F[List[Person]] = | |
for { | |
person <- getPersonMtl[F](id) | |
_ <- log[F](s"fetched $person") | |
siblings <- person.siblings.traverse(getPersonMtl[F](_)) | |
} yield siblings | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment