简体   繁体   中英

Scala Future, flatMap that works on Either

Is there really a way to transform an object of type Future[Either[Future[T1], Future[T2]]] to and object of type Either[Future[T1], Future[T2]] ??

Maybe something like flatMap that works on Either....

I'm trying to make this code work (I have similar code that implements wrapped chain-of actions, but it doesn't involve future. It works, much simpler). The code below is based on that, with necessary modification to make it work for situation that involves futures.

case class WebServResp(msg: String)
case class WebStatus(code: Int)
type InnerActionOutType = Either[Future[Option[WebServResp]], Future[WebStatus]]
type InnerActionSig = Future[Option[WebServResp]] => Either[Future[Option[WebServResp]], Future[WebStatus]]

val chainOfActions: InnerActionSig = Seq(
  {prevRespOptFut => 
    println("in action 1: " + prevRespOptFut)
    //dont care about prev result
    Left(Future.successful(Some(WebServResp("result from 1"))))
  },
  {prevRespOptFut => 
    println("in action 2: " + prevFutopt)
    prevRespOptFut.map {prevRespOpt =>
      //i know prevResp contains instance of WebServResp. so i skip the opt-matching
      val prevWebServResp = prevRespOpt.get
      Left(Some(prevWebServResp.msg + " & " + " additional result from 2"))
    }

    //But the outcome of the map above is: Future[Left(...)]
    //What I want is Left(Future[...])
  }
)

type WrappedActionSig = InnerActionOutType => InnerActionOutType 
val wrappedChainOfActions = chainOfActions.map {innerAction => 
  val wrappedAction: WrappedActionSig = {respFromPrevWrappedAction =>
    respFromPrevWrappedAction match {
      case Left(wsRespOptFut) => {        
        innerAction(wsRespOptFut)       
      }
      case Right(wsStatusFut) => {
        respFromPrevWrappedAction
      }
    }
  }
  wrappedAction
}

wrappedChainOfActions.fold(identity[WrappedActionIOType] _)  ((l, r) => l andThen r).apply(Left(None))

UPDATE UPDATE UPDATE

Based on comments from Didier below ( Scala Future, flatMap that works on Either )... here's a code that works:

//API
case class WebRespString(str: String)
case class WebStatus(code: Int, str: String)
type InnerActionOutType = Either[Future[Option[WebRespString]], Future[WebStatus]]
type InnerActionSig = Future[Option[WebRespString]] => InnerActionOutType

type WrappedActionSig = InnerActionOutType => InnerActionOutType
def executeChainOfActions(chainOfActions: Seq[InnerActionSig]): Future[WebStatus] = {
  val wrappedChainOfActions : Seq[WrappedActionSig] = chainOfActions.map {innerAction => 
    val wrappedAction: WrappedActionSig = {respFromPrevWrappedAction =>
      respFromPrevWrappedAction match {
        case Left(wsRespOptFut) => {        
          innerAction(wsRespOptFut)       }
        case Right(wsStatusFut) => {
          respFromPrevWrappedAction
        }
      }
    }
    wrappedAction
  }  

  val finalResultPossibilities = wrappedChainOfActions.fold(identity[InnerActionOutType] _)  ((l, r) => l andThen r).apply(Left(Future.successful(None)))
  finalResultPossibilities match {
    case Left(webRespStringOptFut) => webRespStringOptFut.map {webRespStringOpt => WebStatus(200, webRespStringOpt.get.str)}
    case Right(webStatusFut) => webStatusFut
  }  
}

//API-USER

executeChainOfActions(Seq(
  {prevRespOptFut => 
    println("in action 1: " + prevRespOptFut)
    //dont care about prev result
    Left(Future.successful(Some(WebRespString("result from 1"))))
  },
  {prevRespOptFut => 
    println("in action 2: " + prevRespOptFut)
    Left(prevRespOptFut.map {prevRespOpt => 
      val prevWebRespString = prevRespOpt.get
      Some(WebRespString(prevWebRespString.str + " & " + " additional result from 2"))
    })
  }  
)).map {webStatus =>
  println(webStatus.code + ":" + webStatus.str)
}

executeChainOfActions(Seq(
  {prevRespOptFut => 
    println("in action 1: " + prevRespOptFut)
    //Let's short-circuit here
    Right(Future.successful(WebStatus(404, "resource non-existent")))
  },
  {prevRespOptFut => 
    println("in action 2: " + prevRespOptFut)
    Left(prevRespOptFut.map {prevRespOpt => 
      val prevWebRespString = prevRespOpt.get
      Some(WebRespString(prevWebRespString.str + " & " + " additional result from 2"))
    })
  }  
)).map {webStatus =>
  println(webStatus.code + ":" + webStatus.str)
}

Thanks, Raka

The type Future[Either[Future[T1], Future[T2]]] means that sometimes later (that's future) one gets an Either, so at that time, one will know which way the calculation will go, and whether one will, still later, gets a T1 or a T2.

So the knowledge of which branch will be chosen ( Left or Right ) will come later. The type Either[Future[T1], Future[T2] means that one has that knowledge now (don't know what the result will be, but knows already what type it will be). The only way to get out of the Future is to wait.

No magic here, the only way for later to become now is to wait, which is done with result on the Future, and not recommended. `

What you can do instead is say that you are not too interested in knowing which branch is taken, as long has it has not completed, so Future[Either[T1, T2]] is good enough. That is easy. Say you have the Either and you would rather not look not but wait for the actual result :

def asFuture[T1, T2](
    either: Either[Future[T1], Future[T2]])(
    implicit ec: ExecutionContext)
 : Future[Either[T1, T2] =  either match {
   case Left(ft1) => ft1 map {t1 => Left(t1)}
   case Right(ft2) => ft2 map {t2 => Right(t2)}
}

You don't have the Either yet, but a future on that, so just flatMap

f.flatMap(asFuture) : Future[Either[T1, T2]]

(will need an ExecutionContext implicitly available)

It seems like you don't actually need the "failure" case of the Either to be a Future ? In which case we can use scalaz (note that the "success" case of an either should be on the right):

import scalaz._
import scalaz.Scalaz._

def futureEitherFutureToFuture[A, B](f: Future[Either[A, Future[B]]])(
  implicit ec: ExecutionContext): Future[Either[A, B]] =
  f.flatMap(_.sequence)

But it's probably best to always keep the Future on the outside in your API, and to flatMap in your code rather than in the client. (Here it's part of the foldLeftM ):

case class WebServResp(msg: String)
case class WebStatus(code: Int)
type OWSR = Option[WebServResp]
type InnerActionOutType = Future[Either[WebStatus, OWSR]]
type InnerActionSig = OWSR => InnerActionOutType

def executeChain(chain: List[InnerActionSig]): InnerActionOutType = 
  chain.foldLeftM(None: OWSR) {
    (prevResp, action) => action(prevResp)
  }

//if you want that same API
def executeChainOfActions(chainOfActions: Seq[InnerActionSig]) =
  executeChain(chainOfActions.toList).map {
    case Left(webStatus) => webStatus
    case Right(webRespStringOpt) => WebStatus(200, webRespStringOpt.get.str)
  }

(If you need "recovery" type actions, so you really need OWSR to be an Either , then you should still make InnerActionOutType a Future[Either[...]] , and you can use .traverse or .sequence in your actions as necessary. If you have an example of an "error-recovery" type action, I can put an example of that here)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM