jmonkey-test/src/main/scala/wow/doge/mygame/utils/ReaderDemo.scala
2021-02-27 11:36:32 +05:30

32 lines
913 B
Scala

package wow.doge.mygame.utils
import cats.data.Reader
import cats.data.ReaderT
import monix.bio.UIO
object ReaderDemo {
type IoReaderT[S, E, A] = ReaderT[UIO, S, Either[E, A]]
val IoReaderT = ReaderT
val t =
ReaderT[UIO, String, Either[Error, Unit]](s => UIO.unit.attempt)
.run("s")
.rethrow
val r: IoReaderT[String, Error, Unit] = IoReaderT(s => UIO.unit.attempt)
val t2 = r.run("s").rethrow
// Kleisli[IO, String, Unit](s => IO.unit)
final case class Environment(str: String, num: Int)
def fun1: Reader[String, UIO[Unit]] = Reader(str => UIO(println(str)))
def fun2: Reader[Int, UIO[Unit]] = Reader(num => UIO(println(num)))
def total: Reader[Environment, UIO[Unit]] =
for {
x <- fun1.local[Environment](_.str)
y <- fun2.local[Environment](_.num)
} yield UIO.parSequence(List(x, y)).void
val io: UIO[Unit] = total.run(Environment("hello", 50))
}