Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
tagless final + free monad
import cats.free.Free
import cats.free.Free.liftF
import cats.{Id, Monad, ~>}
import cats.syntax.option._
// domain layer -----------------------------------------------------------
case class Movie(id: Int, title: String)
sealed trait Query[A]
case class GetMovie(id: Int) extends Query[Option[Movie]]
type QueryF[T] = Free[Query, T]
trait MovieRepoAlg[F[_]] {
def getMovie(id: Int): F[Option[Movie]]
}
class MovieService[F[_]: Monad](alg: MovieRepoAlg[F]) {
def getMovie(id: Int): F[Option[Movie]] = alg.getMovie(id)
}
object QueryFInterpreter extends MovieRepoAlg[QueryF] {
def getMovie(id: Int): QueryF[Option[Movie]] = liftF(GetMovie(id))
}
val MovieServiceImpl = new MovieService(QueryFInterpreter)
// application layer -------------------------------------------------
import monix.eval.Task
val db = Map[Int, Movie](42 -> Movie(42, "A Movie"))
def taskInterpreter: Query ~> Task = new (Query ~> Task) {
def apply[A](fa: Query[A]): Task[A] = fa match {
case GetMovie(id) => Task(db.get(id))
}
}
val program = MovieServiceImpl.getMovie(42).foldMap(taskInterpreter)
// runtime ------------------------------------------------------
import monix.execution.Scheduler.Implicits.global
import scala.concurrent.duration._
import scala.concurrent.Await
Await.result(program.runToFuture, 1.second)
//res0: Option[Movie] = Some(Movie(42,A Movie))
// test environment -------------------------------------------------------
val testMovieRepoImpl = new MovieRepoAlg[Id] {
def getMovie(id: Int): Option[Movie] = Movie(id, s"Movie($id)").some
}
val testMovieServiceImpl = new MovieService(testMovieRepoImpl)
testMovieServiceImpl.getMovie(42)
// Some(Movie(42,Movie(42)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment