Created
August 7, 2018 14:18
-
-
Save gvolpe/56e2cf40cbd06060a6fe76bb6dc1ba22 to your computer and use it in GitHub Desktop.
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.Monad | |
| import cats.effect.{IO, Sync} | |
| import cats.syntax.flatMap._ | |
| import cats.syntax.functor._ | |
| trait Command[F[_]] { | |
| def getStrLn: F[String] | |
| def putStrLn(string: String): F[Unit] | |
| } | |
| class CommandProgram[F[_]: Monad](implicit C: Command[F]) { | |
| def get2StrLns: F[String] = | |
| C.getStrLn.flatMap(a => C.getStrLn.map(b => a + "\n" + b)) | |
| } | |
| class CommandInterpreter[F[_]](implicit F: Sync[F]) extends Command[F] { | |
| override def getStrLn: F[String] = | |
| F.delay { scala.io.StdIn.readLine } | |
| override def putStrLn(string: String): F[Unit] = | |
| F.delay { println(string) } | |
| } | |
| class OtherProgram[F[_]](implicit C: CommandProgram[F]) { | |
| def someLogic: F[String] = C.get2StrLns | |
| } | |
| class SomeProgram[F[_]: Monad](implicit C: Command[F]) { | |
| def someLogic: F[Unit] = C.getStrLn.flatMap(C.putStrLn) | |
| } | |
| implicit val cmd = new CommandInterpreter[IO] | |
| implicit val cmdExt: CommandProgram[IO] = new CommandProgram[IO] | |
| println(new OtherProgram[IO].someLogic.unsafeRunSync()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment