Testing out JmonkeyEngine to make a game in Scala with Akka Actors within a pure FP layer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.2 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package wow.doge.mygame
  2. import cats.data.ReaderT
  3. import monix.bio.UIO
  4. import org.scalatest.funsuite.AnyFunSuite
  5. import monix.execution.Scheduler.Implicits.global
  6. import monix.bio.IO
  7. import cats.mtl.Ask
  8. import cats.mtl.implicits._
  9. import cats.effect.LiftIO
  10. import monix.bio.Task
  11. import cats.data.Kleisli
  12. import monix.bio.IOLift
  13. class ReaderT_Test extends AnyFunSuite {
  14. // type IoReaderT[S, E, A] = ReaderT[UIO, S, Either[E, A]]
  15. // val IoReaderT = ReaderT
  16. // val t =
  17. // ReaderT[UIO, String, Either[Error, Unit]](s => UIO.unit.attempt)
  18. // .run("s")
  19. // .rethrow
  20. // val r: IoReaderT[String, Error, Unit] = IoReaderT(s => UIO.unit.attempt)
  21. // val t2 = r.run("s").rethrow
  22. // Kleisli[IO, String, Unit](s => IO.unit)
  23. case class Environment(str: String, num: Int)
  24. // test("runReaderT_Test") {
  25. // def fun1: ReaderT[UIO, String, Unit] = ReaderT(str => UIO(println(str)))
  26. // def fun2: ReaderT[UIO, Int, Unit] = ReaderT(num => UIO(println(num)))
  27. // def total: ReaderT[UIO, Environment, Unit] =
  28. // for {
  29. // _ <- fun1.local[Environment](_.str)
  30. // _ <- fun2.local[Environment](_.num)
  31. // } yield ()
  32. // val uio: UIO[Unit] = total.run(Environment("hello", 50))
  33. // uio.runSyncUnsafe()
  34. // }
  35. implicit val L =
  36. new LiftIO[ReaderT[IO[String, ?], Environment, *]] {
  37. override def liftIO[A](
  38. ioa: cats.effect.IO[A]
  39. ): ReaderT[IO[String, ?], Environment, A] =
  40. ReaderT(_ => IO.from(ioa).onErrorHandleWith(_ => IO.raiseError("whew")))
  41. // override def liftIO[A](ioa: cats.effect.IO[A]): monix.bio.Task[A] =
  42. // Task.from(ioa)
  43. }
  44. implicit val L2 = new IOLift[ReaderT[IO[String, ?], Environment, *]] {
  45. override def apply[A](
  46. task: monix.bio.Task[A]
  47. ): ReaderT[IO[String, ?], Environment, A] =
  48. ReaderT(_ => IO.from(task).onErrorHandleWith(_ => IO.raiseError("whew")))
  49. }
  50. sealed trait AppError
  51. type RIO[S, E, A] = ReaderT[IO[E, ?], S, A]
  52. type EIO[S, A] = RIO[S, AppError, A]
  53. type AppIO[A] = RIO[Environment, AppError, A]
  54. test("2") {
  55. def fun1: RIO[String, String, Unit] =
  56. ReaderT(s => IO(println(s)).onErrorHandleWith(_ => IO.raiseError("wow")))
  57. def fun2: ReaderT[IO[String, ?], Int, Unit] =
  58. ReaderT(num =>
  59. IO(println(num)).onErrorHandleWith(_ => IO.raiseError("whew"))
  60. )
  61. def fun3: ReaderT[IO[String, ?], Environment, Unit] =
  62. for {
  63. env <- ReaderT.ask[IO[String, ?], Environment]
  64. } yield ()
  65. def test[F[_]]()(implicit A: Ask[F, Int]) = A.ask[Int]
  66. // def synctest[F[_]]
  67. def fun4: ReaderT[IO[String, ?], Environment, Unit] =
  68. for {
  69. env <- ReaderT.ask[IO[String, ?], Environment]
  70. _ <- ReaderT.liftF(
  71. IO(println(env)).onErrorHandleWith(_ => IO.raiseError("wow"))
  72. )
  73. _ <- fun3
  74. } yield ()
  75. val topkek =
  76. test[ReaderT[IO[String, ?], Int, *]]().local[Environment](_.num)
  77. def app: ReaderT[IO[String, ?], Environment, Unit] =
  78. for {
  79. _ <- fun1.local[Environment](_.str)
  80. _ <- fun2.local[Environment](_.num)
  81. _ <- fun3
  82. _ <- fun4
  83. _ <- topkek
  84. } yield ()
  85. val io: IO[String, Unit] = app.run(Environment("hello", 50))
  86. io.attempt.runSyncUnsafe()
  87. }
  88. }