Created
September 2, 2016 03:52
-
-
Save chenharryhua/cb6604c2b3a49919072aebbdf456fbc9 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 scalaz._ | |
import Scalaz._ | |
//scalaz 7.3.0-M4 | |
object MyFreeT extends App { | |
trait MyFunctor[A] | |
case class Get() extends MyFunctor[Int] | |
case class Put(x: Int) extends MyFunctor[String] | |
type MyOps[A] = Free[MyFunctor, A] | |
type MyOpT[A] = FreeT[MyFunctor, \/[Throwable, ?], A] | |
val liftGet: MyOpT[Int] = | |
FreeT.liftF[MyFunctor, \/[Throwable, ?], Int](Get()) | |
val liftPut: MyOpT[String] = | |
FreeT.liftF[MyFunctor, \/[Throwable, ?], String](Put(1)) | |
val forComprehension = for { g <- liftGet; p <- liftPut } yield (g + 1, p) | |
object interpreter extends ~>[MyFunctor, \/[Throwable, ?]] { | |
override def apply[A](a: MyFunctor[A]): \/[Throwable, A] = a match { | |
case Get() => 1.right | |
case Put(x) => x match { | |
case 0 => new Exception().left | |
case 1 => "a".right | |
} | |
} | |
} | |
val kk = forComprehension.foldMap(interpreter) | |
println(kk) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment