Reducing a list of futures

2019-09-10 11:42发布

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?

2条回答
唯我独甜
2楼-- · 2019-09-10 11:56

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)
查看更多
萌系小妹纸
3楼-- · 2019-09-10 12:15
Future.reduce(futures)(_ ::: _)

Documentation

查看更多
登录 后发表回答