Skip to content

Instantly share code, notes, and snippets.

@chenharryhua
Created September 2, 2016 03:52
Show Gist options
  • Save chenharryhua/cb6604c2b3a49919072aebbdf456fbc9 to your computer and use it in GitHub Desktop.
Save chenharryhua/cb6604c2b3a49919072aebbdf456fbc9 to your computer and use it in GitHub Desktop.
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