Skip to content

Instantly share code, notes, and snippets.

@yasuabe
Last active Feb 18, 2019
Embed
What would you like to do?
tagless final
import cats.{Id, Monad}
// domain layer ------------------------------------------------------
case class Movie(id: Int, title: String)
trait MovieRepoSym[F[_]] {
def getMovie(id: Int): F[Option[Movie]]
}
class MovieService[F[_]: Monad](sym: MovieRepoSym[F]) {
def getMovie(id: Int): F[Option[Movie]] =
sym.getMovie(id)
}
// application layer ------------------------------------------------------
import monix.eval.Task
val db = Map[Int, Movie](42 -> Movie(42, "A Movie"))
object TaskInterpreter extends MovieRepoSym[Task] {
def getMovie(id: Int): Task[Option[Movie]] = Task(db.get(id))
}
val service = new MovieService(TaskInterpreter)
val program = service.getMovie(42)
// runtime (the end of the universe) -------------------------------------
import scala.concurrent.Await
import scala.concurrent.duration._
import monix.execution.Scheduler.Implicits.global
Await.result(program.runToFuture, 1.second)
//Some(Movie(42,A Movie))
// test environment -------------------------------------------------------
object TestInterpreter extends MovieRepoSym[Id] {
def getMovie(id: Int): Option[Movie] = Option(Movie(-1, "Dummy"))
}
val id1 = new MovieService(TestInterpreter).getMovie(42)
//Some(Movie(-1,Dummy))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment