Skip to content

Instantly share code, notes, and snippets.

@julien-truffaut
Created October 25, 2018 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save julien-truffaut/a93335b7cbf11798c1d4a0ccbc6846d9 to your computer and use it in GitHub Desktop.
Save julien-truffaut/a93335b7cbf11798c1d4a0ccbc6846d9 to your computer and use it in GitHub Desktop.
Mtl Example
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