Created
January 16, 2017 14:59
-
-
Save Leammas/37c0e170173e0cda3c8d539a80070769 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.data.ReaderT | |
import cats.implicits.{catsStdInstancesForFuture, _} | |
import cats.{Applicative, Monad} | |
import scala.concurrent.ExecutionContext.global | |
import scala.concurrent.duration._ | |
import scala.concurrent.{Await, ExecutionContext, Future} | |
object Foo extends App { | |
implicit val f = catsStdInstancesForFuture(global) | |
trait Repo[F[_]] { | |
def getFoo(x: Int): F[String] | |
} | |
trait Service[F[_]] { | |
def someMethod(s: String): F[Int] | |
} | |
def exec[F[_] : Monad](r: Repo[F], s: Service[F])(implicit ap: Applicative[F]): F[Unit] = { | |
val a = (r.getFoo(1) |@| s.someMethod("foo")).tupled(ap, ap) | |
for { | |
z <- a | |
} yield () | |
} | |
type PimpedFuture[A] = ReaderT[Future, ExecutionContext, A] | |
object PimpedFuture { | |
def apply[T](value: => T): PimpedFuture[T] = ReaderT.apply { implicit ExecutionContext => | |
Future(value) | |
} | |
} | |
object RPF extends Repo[PimpedFuture] { | |
def getFoo(x: Int): PimpedFuture[String] = { | |
PimpedFuture { | |
Thread.sleep(5000) | |
println(s"running getFoo with $x") | |
x.toString | |
} | |
} | |
} | |
object SPF extends Service[PimpedFuture] { | |
def someMethod(s: String): PimpedFuture[Int] = { | |
PimpedFuture { | |
Thread.sleep(10) | |
println(s"running getBar with $s") | |
s.length | |
} | |
} | |
} | |
val pf = exec(RPF, SPF).run(global) | |
Await.ready(pf, 10 seconds) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment