Sequencing both Scalaz WriterT and Either with for-yield

混江龙づ霸主 提交于 2019-12-03 08:37:11
I See Voices

you should wrap your Future monad in the EitherT, sightly modifying your code. It would look like that:

type EFT[α] = EitherT[F, Throwable, α]
type WEFT[α] = WriterT[EFT, Z, α]

def bazA(): WEFT[Int] = WriterT.put[EFT, Z, Int](EitherT.right[F, Throwable, Int](f.point(18)))(z.zero)

def bar(): WEFT[Int] = for {
  a <- bazA
  b <- bazA
} yield a + b

You can also define lift functions (which lifts the value from one monad to your transformer) to avoid boilerplate.

def liftW[A](fa: Future[A]): WLET[A] = {
    WriterT.put[MLT, Z, A](EitherT.right[Future, Throwable, A](fa))(z.zero)
 }

  def bbar(): WLET[Int] = for {
    a ← liftW(6.point[F])
    b ← liftW(6.point[F])
  } yield a + b

I am sure that lift functions are present in scalaZ, but I am always struggling to find them, and it appears that sometimes these are easier to write yourself.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!