如何将Map [A​​,Future [B]]转换为Future [Map [A​​,B]]? [英] How to convert Map[A,Future[B]] to Future[Map[A,B]]?

查看:233
本文介绍了如何将Map [A​​,Future [B]]转换为Future [Map [A​​,B]]?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用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]]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆