Created
May 30, 2017 09:51
-
-
Save anler/1d4be2719c510f11d8f2eb3fc4eececa to your computer and use it in GitHub Desktop.
This file contains 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._, cats.data._, cats.syntax._, cats.implicits._ | |
implicit def readerTMonadError[F[_], I, E](implicit AE: ApplicativeError[ReaderT[F, I, ?], E], M: FlatMap[ReaderT[F, I, ?]]) = | |
new MonadError[ReaderT[F, I, ?], E] { | |
def pure[A](x: A) = AE.pure(x) | |
def handleErrorWith[A](fa: ReaderT[F, I, A])(f: E => ReaderT[F, I, A]) = | |
AE.handleErrorWith(fa)(f) | |
def raiseError[A](e: E) = | |
AE.raiseError[A](e) | |
def flatMap[A, B](fa: ReaderT[F, I, A])(f: A => ReaderT[F, I, B]) = | |
M.flatMap(fa)(f) | |
def tailRecM[A, B](a: A)(f: A => ReaderT[F, I, Either[A, B]]) = | |
M.tailRecM(a)(f) | |
} | |
implicit def readerMonadState[F[_], I, O, S](implicit M: Monad[ReaderT[F, I, ?]], ST: MonadState[F, S]) = | |
new MonadState[ReaderT[F, I, ?], S] { | |
def flatMap[A, B](fa: ReaderT[F, I, A])(f: A => ReaderT[F, I, B]) = M.flatMap(fa)(f) | |
def tailRecM[A, B](a: A)(f: A => ReaderT[F, I, Either[A, B]]) = M.tailRecM(a)(f) | |
def pure[A](a: A) = M.pure(a) | |
def get = ReaderT[F, I, S] { env => ST.get } | |
def set(s: S) = ReaderT[F, I, Unit] { env => ST.set(s) } | |
} | |
def a: ReaderT[StateT[util.Try, String, ?], String, Int] = ??? | |
def b: ReaderT[StateT[util.Try, String, ?], String, Int] = ??? | |
a >> b | |
// output: | |
// error: value >> is not a member of cats.data.ReaderT[[γ$5$]cats.data.StateT[scala.util.Try,String,γ$5$],String,Int] | |
// a >> b | |
// ^ | |
// |
This file contains 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._, cats.data._, cats.syntax._, cats.implicits._ | |
implicit def readerTMonadError[F[_], I, E](implicit AE: ApplicativeError[ReaderT[F, I, ?], E], M: FlatMap[ReaderT[F, I, ?]]) = | |
new MonadError[ReaderT[F, I, ?], E] { | |
def pure[A](x: A) = AE.pure(x) | |
def handleErrorWith[A](fa: ReaderT[F, I, A])(f: E => ReaderT[F, I, A]) = | |
AE.handleErrorWith(fa)(f) | |
def raiseError[A](e: E) = | |
AE.raiseError[A](e) | |
def flatMap[A, B](fa: ReaderT[F, I, A])(f: A => ReaderT[F, I, B]) = | |
M.flatMap(fa)(f) | |
def tailRecM[A, B](a: A)(f: A => ReaderT[F, I, Either[A, B]]) = | |
M.tailRecM(a)(f) | |
} | |
// implicit def readerMonadState[F[_], I, O, S](implicit M: Monad[ReaderT[F, I, ?]], ST: MonadState[F, S]) = | |
// new MonadState[ReaderT[F, I, ?], S] { | |
// def flatMap[A, B](fa: ReaderT[F, I, A])(f: A => ReaderT[F, I, B]) = M.flatMap(fa)(f) | |
// def tailRecM[A, B](a: A)(f: A => ReaderT[F, I, Either[A, B]]) = M.tailRecM(a)(f) | |
// def pure[A](a: A) = M.pure(a) | |
// def get = ReaderT[F, I, S] { env => ST.get } | |
// def set(s: S) = ReaderT[F, I, Unit] { env => ST.set(s) } | |
// } | |
def a: ReaderT[StateT[util.Try, String, ?], String, Int] = ??? | |
def b: ReaderT[StateT[util.Try, String, ?], String, Int] = ??? | |
a >> b | |
// output: | |
// scala.NotImplementedError: an implementation is missing | |
// at scala.Predef$.$qmark$qmark$qmark(Predef.scala:230) | |
// at .a(<pastie>:43) | |
// ... 50 elided | |
// | |
// which is ok since I didn't implement 'a' and 'b' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment