问题
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