在Scala Akka future中,map和flatMap有什么区别?
在普通的 Scala 中,map 和 flatMap 的不同之处在于,flatMap 将返回一个扁平化到列表中的数据的可迭代对象。 然而在Akka文档中,map和flatMap似乎做了一些不同的事情?
http://akka.io/docs/akka/1.1/scala/futures.html
它说“通常这工作得很好,因为这意味着运行快速函数的开销非常小。如果函数有可能花费大量时间来处理,那么最好有这同时进行,为此,我们使用 flatMap:“
val f1 = Future {
"Hello" + "World"
}
val f2 = f1 flatMap {x =>
Future(x.length)
}
val result = f2.get()
有人可以解释一下 Akka future 中的 map 和 flatMap 之间有什么区别吗?
in normal Scala map and flatMap are different in that flatMap will return a iterable of the data flattened out into a list.
However in the Akka documentation, map and flatMap seem to do something different?
http://akka.io/docs/akka/1.1/scala/futures.html
It says "Normally this works quite well as it means there is very little overhead to running a quick function. If there is a possibility of the function taking a non-trivial amount of time to process it might be better to have this done concurrently, and for that we use flatMap:"
val f1 = Future {
"Hello" + "World"
}
val f2 = f1 flatMap {x =>
Future(x.length)
}
val result = f2.get()
Can someone please explain what is the difference between map and flatMap here in Akka futures?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在“正常”Scala 中(如您所说),map 和 flatMap 与列表无关(例如检查 Option)。
阿列克谢给了你正确的答案。现在,如果你想知道为什么我们需要两者,它允许在编写 future 时使用漂亮的
for
语法。给定类似的内容:编译器将其重写为:
如果遵循方法签名,您应该看到表达式将返回
Future[A]
类型的内容。假设现在仅使用映射,编译器可以执行以下操作:
然而,结果应该是
Future[Future[A]]
类型。这就是为什么你需要将其压平。要了解背后的概念,这是我读过的最好的介绍之一:
http://www.codecommit.com/blog/ruby/monads-are-not-metaphors
In "normal" Scala (as you say), map and flatMap have nothing to do with Lists (check Option for example).
Alexey gave you the correct answer. Now, if you want to know why we need both, it allows the nice
for
syntax when composing futures. Given something like:The compiler rewrites it as:
If you follow the method signature, you should see that the expression will return something of type
Future[A]
.Suppose now only map was used, the compiler could have done something like:
However, the result whould have been of type
Future[Future[A]]
. That's why you need to flatten it.To learn about the concept behind, here is one the best introduction I've read:
http://www.codecommit.com/blog/ruby/monads-are-not-metaphors
类型基本上是:
The type, basically:
我在这里粘贴这两种方法的实现。
英文术语的差异如下
并返回函数的结果作为新的 future
与实现的区别在于,当 Promise 完成时,flatMap 实际上会调用带有结果的函数。
对于一篇精彩的文章,请阅读:http//danielwestheide.com/blog/2013/01/16/the-neophytes-guide-to-scala-part-9-promises-and-futures-in-practice.html
I am pasting the implementation of the two methods here.
the difference in english terms is below
and returns the result of the function as the new future
From implementation the difference is that flatMap actually calls the function with result when the promise completes.
For a great article read: http//danielwestheide.com/blog/2013/01/16/the-neophytes-guide-to-scala-part-9-promises-and-futures-in-practice.html