Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 将项目添加到递归中的Future[List]_Scala_Recursion_Scala Collections - Fatal编程技术网

Scala 将项目添加到递归中的Future[List]

Scala 将项目添加到递归中的Future[List],scala,recursion,scala-collections,Scala,Recursion,Scala Collections,我对递归中的未来列表有一个问题 当我在没有Futures的情况下实现这个方法时,我使用ListBuffer,然后将项目添加到列表中 val filtered = ListBuffer.empty[PostMD] filtered ++= postMd.filter(_.fromID == userID) 现在我正试图用Futures实现它,但我找不到类似的解决方案 处理未来列表的最佳方式是什么 def getData(url: String, userID: String)

我对递归中的未来列表有一个问题

当我在没有Futures的情况下实现这个方法时,我使用ListBuffer,然后将项目添加到列表中

    val filtered = ListBuffer.empty[PostMD]
    filtered ++= postMd.filter(_.fromID == userID)
现在我正试图用Futures实现它,但我找不到类似的解决方案 处理未来列表的最佳方式是什么

 def getData(url: String, userID: String) = {

val filtered: (List[PostMD]) => Future[List[PostMD]] = Future[List[PostMD]]

def inner(url: String): Unit = {

  val chunk: Future[JsValue]  = BusinessLogic.Methods.getJsonValue(url)

  val postMd: Future[List[PostMD]] = for {
    x <- chunk.map(_.\("data").as[List[JsValue]])
    y <- x.map(_.\("data").as[PostMD])
  } yield y


  filtered = postMd.map(_.filter(_.fromID == userID))   // <- returned  Future[List[PostMD]]

  val next: String = (chunk.map(_.\("paging").\("next"))).toString

 if (next != null) inner(next)
}
inner(url)
filtered

 }
谢谢


miki

我试着用随机数生成做你想做的事

import scala.concurrent.{Await, Future}
import scala.util.Random

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._

val RANDOM = new Random()

def futureRec(num: Int, f: Future[List[Integer]]): Future[List[Integer]] = {
  if(num == 0) {
    f
  } else {
    f.flatMap(l => {
      futureRec(num - 1, Future.successful(RANDOM.nextInt() :: l))
    })
  }
}

val futureResult = futureRec(5, Future.successful(Nil))

Await.result(futureResult, 5 minutes)
所以我想做的是,你想要这样的东西:

def getData(url: String, userID: String):Future[List[PostMD]] = {
  def inner(url: String, f: Future[List[PostMD]]): Future[List[PostMD]] = {

      val chunk: Future[JsValue]  = ???
      chunk.flatMap(ch => {
        val postMd = (ch \ "data").\\("data").map(_.as[PostMD]).toList
        val relatedPostMd = postMd.filter(_.fromID == userID)
        val next: String = (ch.\("paging").\("next")).as[String]
        if (next != null)
           inner(next, f.map(l => l ++ relatedPostMd))
        else
           f.map(l => l ++ relatedPostMd)
      })
  }

  inner(url, Future.successful(Nil))
}

你能分享一个JSON的例子吗?另外,看起来您正在使用play json。是的,我使用play Json和play WS来获取数据。我认为他们有更多的方法来做到这一点。。。json是facebook api json这是一个示例