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.

61 lines
1.8 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package wow.doge.mygame.implicits
  2. import cats.Functor
  3. import cats.Show
  4. import cats.data.Kleisli
  5. import cats.effect.Resource
  6. import cats.syntax.show._
  7. import monix.bio.IO
  8. import monix.bio.Task
  9. import monix.bio.UIO
  10. trait CatsExtensions {
  11. implicit class KleisliCompanionExt(k: Kleisli.type) {
  12. def io[S, E, A](f: S => IO[E, A]): Kleisli[UIO, S, Either[E, A]] =
  13. k.apply(s => f(s).attempt)
  14. }
  15. implicit class KleisliExt[S, E, A](k: Kleisli[UIO, S, Either[E, A]]) {
  16. def runIO(s: S) = k.run(s).rethrow
  17. }
  18. implicit class ResourceCompanionExt(r: Resource.type) {
  19. // def ioMake[UIO, R](acquire: )(release: ) = r.make()()
  20. def ioMake[E: Show, A](
  21. acquire: IO[E, A]
  22. )(
  23. release: A => UIO[Unit]
  24. )(implicit F: Functor[UIO]): Resource[UIO, Either[E, A]] =
  25. r.make(acquire.attempt)(a =>
  26. IO.fromEither(a)
  27. .onErrorHandleWith(err => IO.terminate(new Exception(err.show)))
  28. .flatMap(release)
  29. )
  30. val acq = IO(1).onErrorHandleWith(_ => IO.raiseError(""))
  31. val res =
  32. ioMake(acq)(_ => IO.unit)
  33. val result = res
  34. .use {
  35. case Left(value) => Task(Left(value))
  36. case Right(value) => Task(Right(value))
  37. }
  38. .hideErrors
  39. .rethrow
  40. // IO.unit.bracket()
  41. }
  42. implicit class ResourceExt[E, A](k: Resource[UIO, Either[E, A]]) {
  43. // def runIO(s: S) = k.run(s).rethrow
  44. // k.use
  45. // : IO[E, B]
  46. // def useIO[B](f: Either[E, A] => IO[E, B]) =
  47. // k.use(f).rethrow
  48. // type test[A] = Tuple2[*, Double]
  49. type IoResource[X, D] = Resource[IO[X, *], D]
  50. val x: Resource[IO[String, *], String] =
  51. Resource.make(IO.raiseError(""))(_ => IO.unit)
  52. // x.use(s => Task.unit)
  53. val x2: IoResource[String, String] =
  54. Resource.make(UIO(""))(_ => IO.unit)
  55. }
  56. }