Reducing a list of futures

♀尐吖头ヾ 提交于 2019-12-11 23:34:45

问题


I have long running function that returns a future as follows:

def longRunningFunction(signs: List[String], numOfWords: Int)
    : Future[List[(String, Int)]] = Future{ /* computation */ }

I need to reduce the output of the Future as follows:

val all = (6 to 24).map(i => longRunningFunction(signs, i))
                   .reduce(_ ::: _)

But this does not seem to work. Any thoughts?


回答1:


Future.reduce(futures)(_ ::: _)

Documentation




回答2:


Is this the thing you're looking for?

def longRunningFunction(signs: List[String], numOfWords: Int): Future[List[(String, Int)]] = ???

val all: IndexedSeq[Future[List[(String, Int)]]] = (6 to 24).map(i => longRunningFunction(signs, i))
val result: Future[IndexedSeq[(String, Int)]] = Future.sequence(all).map(_.flatten)


来源:https://stackoverflow.com/questions/38192239/reducing-a-list-of-futures

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!