Last active
July 12, 2016 06:03
-
-
Save aeyakovenko/46145a241919a39625f8b59f18bb7d8d 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
package org.apache.mesos.edsl | |
import cats.{Applicative,Functor} | |
import cats.data.{XorT,StateT} | |
package object control { | |
type ErrorTStateT[F[_], S, A] = XorT[({type l[X] = StateT[F, S, X]})#l, String, A] | |
def bail[F[_], S, A](msg: String)(implicit I1: Applicative[F], I2: Functor[({type l[X] = StateT[F, S, X]})#l]): ErrorTStateT[F, S, A] = | |
XorT.left[({type l[X] = StateT[F, S, X]})#l, String, A](StateT.pure[F, S, String](msg)) | |
def state[F[_], S, A](f: (S) => (S,A))(implicit I1: Applicative[F], I2: Functor[({type l[X] = StateT[F, S, X]})#l]): ErrorTStateT[F, S, A] = | |
XorT.right[({type l[X] = StateT[F, S, X]})#l,String,A](StateT.apply[F, S, A]({ s => I1.pure(f(s)) })) | |
} | |
package object test { | |
import cats.free.{Trampoline} | |
type TestM[A] = control.ErrorTStateT[Trampoline, Int, A] | |
def bail[A](msg:String):TestM[A] = control.bail(msg) | |
def state[A](f: Int => (Int,A)):TestM[A] = control.state(f) | |
def get:TestM[Int] = state({ s => (s,s)}) | |
def put(v:Int):TestM[Unit] = state({ _ => (v,())}) | |
def inc: TestM[Int] = | |
for { | |
v <- get | |
_ <- put(v + 1) | |
} yield(v) | |
} | |
/* | |
scala> inc | |
res1: org.apache.mesos.edsl.test.TestM[Int] = XorT(cats.data.StateT@2e1cc1b7) | |
scala> inc.toEither | |
res2: cats.data.StateT[cats.free.Trampoline,Int,Either[String,Int]] = cats.data.StateT@2d533fef | |
scala> inc.toEither.run(1) | |
res3: cats.free.Trampoline[(Int, Either[String,Int])] = Gosub(Pure(<function1>),<function1>) | |
scala> inc.toEither.run(1).run | |
<console>:15: error: could not find implicit value for parameter S: cats.Comonad[Function0] | |
inc.toEither.run(1).run | |
^ | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment