如何用猫从 Future[Either[String, Int]] 制作 EitherT[Future, String, Int]?

How to make EitherT[Future, String, Int] from Future[Either[String, Int]] with cats?

我有这个代码:

  type Response[A] = EitherT[Future, String, A]

  val powerLevels = Map(
    "Jazz" -> 6,
    "Bumblebee" -> 8,
    "Hot Rod" -> 10
  )
  def getPowerLevel(autobot: String): Response[Int] = {

    val result = Future {
      powerLevels.get(autobot) {
        case Some(number) => Right(number)
        case None         => Left(s"Can't get connect to $autobot")
      }
    }

  }

我不明白如何将函数 getPowerLevel (Future[Either[String, Int]]) 中的计算结果转换为 (Writer 正确地转换为 Response[Int] 类型。我想调用powerLevels.get(autobot)Future.

正如@Luis 指出的那样,您只需要使用 EitherT.apply:

import cats.data.EitherT
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global

import cats.implicits._

  type Response[A] = EitherT[Future, String, A]

  val powerLevels = Map(
    "Jazz" -> 6,
    "Bumblebee" -> 8,
    "Hot Rod" -> 10
  )

  def getPowerLevel(autobot: String): Response[Int] = {

      val result = Future {
        powerLevels.get(autobot) match {
          case Some(number) => Right(number)
          case None         => Left(s"Can't get connect to $autobot")
        }
      }
     EitherT(result)
    }

Monad 转换器将可堆叠的 monad 转换为 return 可组合的 monad。 例如,在这种情况下 EitherT[Future, String, A] 将采用 Future[Either[String, A]] 到 return 一个可组合的 monad。

尽管其他解决方案也能很好地满足这个要求,但我们可以利用 Either 中的 cond API 来更简洁地编写它:

  def getPowerLevel(autobot: String): Response[Int] = {
    val powerLevel = powerLevels.get(autobot)
    EitherT(Future(Either.cond(powerLevel.isDefined, powerLevel.get, s"$autobot unreachable")))
  }