Reputation: 68640
In Scalaz, is there an easy way to convert an instance of Writer[W, A]
(which is an alias for WriterT[Id, W, A])
to WriterT[F, W, A]
?
I'm looking for something similar to the optionT
function combined with point
, but for writers instead:
E.g.
// Similar situation, but using OptionT
val opt: Option[String] = Some("log")
val optT: OptionT[IO, String] = optionT(opt.point[IO])
// Case in hand, using WriterT
val w: Writer[String, Unit] = "log".tell
val wt: WriterT[IO, String, Unit] = ???
// Similar scenario
val w: Writer[String, Int] = 3.set("log")
val wt: WriterT[IO, String, Int] = ???
Upvotes: 2
Views: 320
Reputation: 139038
Some monadic types have lift
methods that help with this kind of operation:
import scalaz._, Scalaz._, effect.IO
val stateIO: StateT[IO, Int, Unit] = put(10).lift[IO]
Writer
doesn't, but you can use the Hoist
instance for WriterT
to accomplish the same thing:
type StringWriter[F[_], A] = WriterT[F, String, A]
def fromId[F[_]: Applicative]: Id ~> F = new (Id ~> F) {
def apply[A](a: A) = a.point[F]
}
val w: Writer[String, Unit] = "log".tell
val wt: WriterT[IO, String, Unit] = Hoist[StringWriter].hoist(fromId[IO]).apply(w)
This isn't terribly convenient, but that's something you have to get used to when working with monad transformers.
Upvotes: 5