[英]Flatten a Set of Future List into one Future List
I'm trying to call persistenceService.find()
returning a Future[List[Scan]]
foreach email passed to my method findByEmails
which should return the merged Future[List[Scan]]
of every calls to persistenceService.find()
我正在尝试调用persistenceService.find()
返回传递给我的方法findByEmails
每封电子邮件的Future[List[Scan]]
电子邮件应返回对persistenceService.find()
的每次调用的合并后的Future[List[Scan]]
So currently, I have this: 所以目前,我有这个:
def findByEmails(emails: Set[String]): Future[List[Scan]] = {
val results: Set[Future[List[Scan]]] = emails.map(email => persistenceService.find("report.commitsStats.contributors." + email -> BSONDocument("$exists" -> true)))
}
I can't figure out how to merge each Future[List[Scan]]
in the results
Set so my method return one Future[List[Scan]]
. 我不知道如何合并results
集中的每个Future[List[Scan]]
,所以我的方法返回一个Future[List[Scan]]
。
Any thoughts? 有什么想法吗?
You can use Future.sequence
to combine a list of futures, then a flatten on the list should be enough, the code might look similar to this not tested code: 您可以使用Future.sequence
组合一个期货列表,然后在列表上进行展平就足够了,该代码可能类似于未经测试的代码:
def f(str: String): Future[List[Scan]] = ???
def g(emails: List[String]): Future[List[Scan] = {
val futures: List[Future[list[Scan]]] = emails.map(f)
val futureListList: Future[List[List[Scan]] = Future.sequence(futures)
futureListList.map(_.flatten)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.