Last active
February 18, 2019 12:54
-
-
Save yasuabe/ec93ff8dea90e889f485e9a75dd070a3 to your computer and use it in GitHub Desktop.
tagless final
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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