I am working with cats and I want to transform my val x: State[A, B]
to StateT[IO, A, B]
. Note: IO is from cats-effects.
How to do this elegantly?
Try mapK
in combination with cats.arrow.FunctionK.lift
:
x.mapK(lift(IO.eval))
Full compilable code snippet:
import cats.effect.IO
import cats.data.{State, StateT}
import cats.arrow.FunctionK.lift
object InjectIdIO {
def i[S, V](x: State[S, V]): StateT[IO, S, V] = x.mapK(lift(IO.eval))
}
This works because State[S, A]
is actually StateT[Eval, S, A]
, and you want to replace the Eval
by IO
- this is what the mapK
is usually for.
Another alternative with kind-projector
:
x.mapK(Lambda[Eval ~> IO](IO.eval(_)))
Try
def liftState[A, B](state: State[A, B]): StateT[IO, A, B] =
StateT[IO, A, B] { s => IO.eval(state.run(s)) }
for example
val x: State[Int, String] = State(int => (int, "foo"))
liftState(x)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.