Guest User

Untitled

a guest
Feb 19th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. import cats._, cats.data._, cats.implicits._
  2. import cats.free.Free
  3. import cats.arrow.FunctionK
  4. import monix.eval.Coeval
  5. object ParserForFreeMonad extends App {
  6. trait MyAlgebraF[A]
  7. final case class Append(cmd: String) extends MyAlgebraF[Int]
  8. final case class Upsert(cmd: String) extends MyAlgebraF[Unit]
  9.  
  10. type MyAlgebra[A] = Free[MyAlgebraF, A]
  11.  
  12. val nt = Lambda[FunctionK[MyAlgebraF, Coeval]] {
  13. case Append(cmd) => Coeval { println(cmd); 1 }
  14. case Upsert(cmd) => Coeval { println(cmd) }
  15. }
  16.  
  17. val prog = for {
  18. a <- Free.liftF(Append("append"))
  19. u <- Free.liftF(Upsert("upsert"))
  20. } yield a
  21.  
  22. println(prog.foldMap(nt).runTry)
  23. }
Add Comment
Please, Sign In to add comment