Skip to content

Instantly share code, notes, and snippets.

@yasuabe
Last active February 18, 2019 12:54
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 yasuabe/ec93ff8dea90e889f485e9a75dd070a3 to your computer and use it in GitHub Desktop.
Save yasuabe/ec93ff8dea90e889f485e9a75dd070a3 to your computer and use it in GitHub Desktop.
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