Scala for comprehension - 合并AbstractSeq的未来

时间:2018-01-20 16:59:26

标签: scala for-comprehension

具有以下代码:

def getListFull: Future[org.json4s.JsonAST.JValue] = {

  val windowSize = 8

  val result = for {
    response <- getResponse()
    if (response \ "response_code").extract[Int] == 0
    maxLine <- Future{(response \ "max_line").extract[Int]}
    res <- Future.traverse(0 until maxLine)(i => getResponse(..., i,...))
  } yield res

  Future.reduceLeft(result)(_ merge _)

  }
}

我收到以下编译错误:

Error:(91, 48) Cannot construct a collection of type scala.collection.AbstractSeq[org.json4s.JsonAST.JValue] with elements of type org.json4s.JsonAST.JValue based on a collection of type scala.collection.AbstractSeq[Int].
    res <- Future.traverse(0 until maxLine)(i => getResponse(

Error:(91, 48) not enough arguments for method traverse: (implicit cbf: scala.collection.generic.CanBuildFrom[scala.collection.AbstractSeq[Int],org.json4s.JsonAST.JValue,scala.collection.AbstractSeq[org.json4s.JsonAST.JValue]], implicit executor: scala.concurrent.ExecutionContext)scala.concurrent.Future[scala.collection.AbstractSeq[org.json4s.JsonAST.JValue]].
Unspecified value parameters cbf, executor.
    res <- Future.traverse(0 until maxLine)(i => getResponse(

Error:(98, 25) type mismatch;
found   : scala.concurrent.Future[Nothing]
required: 
scala.collection.immutable.Iterable[scala.concurrent.Future[?]]
  Future.reduceLeft(result)(_ merge _)

函数getResponse的签名是:

def getResponse(restUrl: String,
              urlParams: Map[String, String] = Map("" -> ""),
              connectTimeout: Int = 5000,
              readTimeout: Int = 5000,
              requestMethod: String = "GET"): Future[org.json4s.JsonAST.JValue] = Future {

目标是通过 getResponse 使用不同的参数多次调用REST API,并合并生成的JSON。

提前致谢!

0 个答案:

没有答案