gpt4 book ai didi

scala - 结合 future ,期权和期权进行理解

转载 作者:行者123 更新时间:2023-12-04 03:21:28 24 4
gpt4 key购买 nike

我有一组返回不同类型的方法:

Either[ErrorResponse, X]
Future[Either[ErrorResponse, X]]
Option[ErrorResponse]

这些方法需要先前方法的结果来执行其计算。方法:
type Parameters = Map[String, String]

// allows me to flatmap on an either
implicit def toRightProjection[Failure, Success](e: Either[Failure, Success]) =
e.right

// converts anything to a future
implicit def toFuture[T](t: T) =
Future.successful(t)

// retrieves the request paramters from the given request
def requestParameters(request: RequestHeader): Either[ErrorResponse, Parameters] = ???

// retrieves the response type from the given parameters
def responseType(p: Parameters): Either[ErrorResponse, String] = ???

// retrieves the client id from the given parameters
def clientId(p: Parameters): Either[ErrorResponse, String] = ???

// retrieves the client using the given client id
def client(clientId: String): Future[Either[ErrorResponse, Client]] = ???

// validates the response type of the client
def validateResponseType(client: Client, responseType: String): Option[ErrorResponse] = ???

我可以将它们与以下内容结合在一起以进行理解(请注意,我写下了一些类型以阐明计算的特定部分的内容)。
val result: Either[ErrorResponse, Future[Either[ErrorResponse, Client]]] =
for {
parameters <- requestParameters(request)
clientId <- clientId(parameters)
responseType <- responseType(parameters)
} yield {
val result: Future[Either[ErrorResponse, Either[ErrorResponse, Client]]] =
for {
errorOrClient <- client(clientId)
client <- errorOrClient
} yield validateResponseType(client, responseType).toLeft(client)

result.map(_.joinRight)
}

val wantedResult: Future[Either[ErrorResponse, Client]] =
result.left.map(Future successful Left(_)).merge

上面的代码很乱,我觉得可以用不同的方式完成。我读过有关monad和monad变压器的信息。这些概念对我来说还很新,我无法理解。

大多数示例仅处理两种类型的结果: Either[X, Y]Future[Either[X, Y]]。我仍然觉得很难对付它。

如何替换上面的内容,写得很好而干净?

这样的事情太棒了(我不确定这是否可能):
val result: Future[Either[ErrorResponse, Client]] =
for {
parameters <- requestParameters(request)
clientId <- clientId(parameters)
responseType <- responseType(parameters)
client <- client(clientId)
_ <- validateResponseType(client, responseType)
}

最佳答案

好,这是我的尝试:

import scalaz._, Scalaz._

implicit val futureMonad = new Monad[Future] {
override def point[A](a: ⇒ A): Future[A] = future(a)

override def bind[A, B](fa: Future[A])(f: A ⇒ Future[B]): Future[B] =
fa.flatMap(f)
}

import EitherT._
val result: EitherT[Future, ErrorResponse, Client] =
for {
parameters <- fromEither(Future(requestParameters(request)))
clientId <- fromEither(Future(clientId(parameters)))
responseType <- fromEither(Future(responseType(parameters)))
client <- fromEither(client(clientId))
response <- fromEither[Future, ErrorResponse, Client](Future(validateResponseType(client, responseType).toLeft(client)))
} yield response

val x: Future[\/[ErrorResponse, Client]] = result.run

关于scala - 结合 future ,期权和期权进行理解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19257060/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com