Skip to content

Instantly share code, notes, and snippets.

@francescofrontera
Last active April 23, 2020 16:47
Show Gist options
  • Save francescofrontera/fa534103e16ae5117d7dd1999c69c7fc to your computer and use it in GitHub Desktop.
Save francescofrontera/fa534103e16ae5117d7dd1999c69c7fc to your computer and use it in GitHub Desktop.
Simple Cats Slick Ops using EitherT or other cats data types over DBIO..
import scala.concurrent.ExecutionContext
import scala.concurrent.ExecutionContext.Implicits._
import slick.dbio.DBIO
import cats.{Functor, Monad}
object SlickCatsOps {
implicit def functor(implicit ex: ExecutionContext): Functor[DBIO] = new Functor[DBIO] {
override def map[A, B](fa: DBIO[A])(f: A => B): DBIO[B] = fa map f
}
implicit def monadic(implicit ex: ExecutionContext): Monad[DBIO] = new Monad[DBIO] {
override def pure[A](x: A): DBIO[A] = DBIO.successful(x)
override def flatMap[A, B](fa: DBIO[A])(f: A => DBIO[B]): DBIO[B] = fa flatMap f
override def tailRecM[A, B](a: A)(f: A => DBIO[Either[A, B]]): DBIO[B] =
f(a) flatMap {
case Left(a1) => tailRecM(a1)(f)
case Right(b) => DBIO.successful(b)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment