为了理解未来和要么

时间:2017-11-21 17:44:09

标签: scala monads for-comprehension scala-2.12

我正在努力学习如何以一种漂亮的monadic方式编写一系列异步进程。该过程的每个步骤都可能失败,因此它正在检索Future[Either[String, T]]

def firstStep(input: Int): Future[Either[String, Long]] = ???
def secondStep(input: Long): Future[Either[String, String]] = ???
def thirdStep(input: String): Future[Either[String, Double]] = ???

鉴于这些功能,我想像这样编写它们

def process(input: Int): Future[Either[String Double]] = {
     for{
        res1 <- firstStep(input)
        res2 <- secondStep(res1)
        res3 <- thirdStep(res2)
     } yield res3
}

但这不起作用,因为每个部分结果都是Either[String, T],我需要的是T本身(或者只是停止执行并返回Left,如果这是情况)。

如何以漂亮的monadic方式组合这些函数(使用for-comprehensions)?

1 个答案:

答案 0 :(得分:3)

EitherT monad变换器可以帮助猫(或双关语)或者scalaz:

import cats._
import cats.implicits._
import cats.EitherT

def process(input: Int): Future[Either[String, Double]] = {
   val res = for {
      res1 <- EitherT(firstStep(input))
      res2 <- EitherT(secondStep(res1))
      res3 <- EitherT(thirdStep(res2))
   } yield res3
   res.value
}
相关问题