加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 将期货,期权和期权结合在一起以进行理解

发布时间:2020-12-16 19:07:21 所属栏目:安全 来源:网络整理
导读:我有一组返回不同类型的方法: Either[ErrorResponse,X]Future[Either[ErrorResponse,X]]Option[ErrorResponse] 这些方法需要先前方法的结果来执行计算.方法: type Parameters = Map[String,String]// allows me to flatmap on an eitherimplicit def toRig
我有一组返回不同类型的方法:

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

上面的代码是相当凌乱,我觉得这可以做不同的.我读了关于单子和单子变压器.这些概念对我来说是非常新鲜的,我不能让我的头脑.

大多数例子只处理两种类型的结果:[X,Y]和未来[[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,Client](Future(validateResponseType(client,responseType).toLeft(client)))
  } yield response

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读