我一直在使用Scala Akka库,遇到了一些问题。如标题所示,我需要将 Map [A,Future [B]] 转换为 Future [Map [A,B]] 。我知道可以将 Future.sequence 用于列表之类的Iterable,但这在这种情况下不起作用。
I've been working with the Scala Akka library and have come across a bit of a problem. As the title says, I need to convert Map[A, Future[B]] to Future[Map[A,B]]. I know that one can use Future.sequence for Iterables like Lists, but that doesn't work in this case.
我想知道:Scala中有没有一种干净的方法来进行这种转换?
I was wondering: is there a clean way in Scala to make this conversion?
推荐答案看看这是否对您有用:
See if this works for you:
val map = Map("a" -> future{1}, "b" -> future{2}, "c" -> future{3}) val fut = Future.sequence(map.map(entry => entry._2.map(i => (entry._1, i)))).map(_.toMap)这个想法是将地图映射到可迭代获取地图键的元组和与该键相关的未来结果。从那里,您可以顺序可迭代的 ,然后获得总计的未来,将其映射并通过 toMap将 Tuples 的 Iterable 转换为地图。
The idea is to map the map to an Iterable for a Tuple of the key of the map and the result of the future tied to that key. From there you can sequence that Iterable and then once you have the aggregate Future, map it and convert that Iterable of Tuples to a map via toMap.
现在,这种方法的另一种选择是尝试执行与 sequence 函数相似的操作,进行了一些调整。您可以像这样编写 sequenceMap 函数:
Now, an alternative to this approach is to try and do something similar to what the sequence function is doing, with a couple of tweaks. You could write a sequenceMap function like so:
def sequenceMap[A, B](in: Map[B, Future[A]])(implicit executor: ExecutionContext): Future[Map[B, A]] = { val mb = new MapBuilder[B,A, Map[B,A]](Map()) in.foldLeft(Promise.successful(mb).future) { (fr, fa) => for (r <- fr; a <- fa._2.asInstanceOf[Future[A]]) yield (r += ((fa._1, a))) } map (_.result) }然后在这样的示例中使用它:
And then use it in an example like this:
val map = Map("a" -> future{1}, "b" -> future{2}, "c" -> future{3}) val fut = sequenceMap(map) fut onComplete{ case Success(m) => println(m) case Failure(ex) => ex.printStackTrace() }这可能比第一个示例效率更高,因为它创建的中间集合较少,对 ExecutionContext 的命中较少。
This might be slightly more efficient than the first example as it creates less intermediate collections and has less hits to the ExecutionContext.
更多推荐
如何将Map [A,Future [B]]转换为Future [Map [A,B]]?
发布评论