Scala 中的“flatmap that s***”惯用表达来自哪里?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8559537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Where does the "flatmap that s***" idiomatic expression in Scala come from?
提问by Guillaume Belrose
What is so powerful about flatmap that it deserves such a place in the Scala folklore?
flatmap 有什么强大之处,值得在 Scala 民间传说中占有一席之地?
采纳答案by Jens Schauder
The reasoning behind this phrase is that you can replace a lot of tedious if/then/else code you would write with calls to flatMap (and other higher order functions).
这句话背后的原因是,您可以使用对 flatMap(和其他高阶函数)的调用来替换大量繁琐的 if/then/else 代码。
This is especially true for Options (see http://tonymorris.github.io/blog/posts/scalaoption-cheat-sheet/)
对于选项尤其如此(参见http://tonymorris.github.io/blog/posts/scalaoption-cheat-sheet/)
But it applies to other monads as well (although I have to admit, I don't exactly understand the details yet myself)
但它也适用于其他 monad(虽然我不得不承认,我自己还没有完全理解细节)
Imagine the situation where you have a collection for which you want to apply a function (or a series of functions) where each function might return null. When you actually use null you code will be riddled with null checks. But if you use Options instead of values, you can just flatmap the values with the desired functions, chaining the functions in the case of multiple functions and get a collection with just the results that aren't null, which in many cases is exactly what you want.
想象一下,您有一个集合,您想为其应用一个函数(或一系列函数),其中每个函数可能返回 null。当您实际使用 null 时,您的代码将充斥着 null 检查。但是,如果您使用选项而不是值,则可以使用所需的函数对值进行平面映射,在多个函数的情况下链接函数并获得仅包含不为空的结果的集合,这在许多情况下正是如此你要。
Since that description is rather convoluted the shorter advice "just flatmap that shit" established itself.
由于该描述相当复杂,因此更短的建议“只是平面图那个狗屎”确立了自己。
回答by Daniel C. Sobral
The story I heard was that two preeminent Scala programmers were pairing when one of them started writing some code like this:
我听到的故事是,当两个杰出的 Scala 程序员结对时,其中一个开始编写如下代码:
option match {
case Some ...
At which point the other said "What is this? Amateur hour? Flat map that shit!"
这时另一个人说:“这是什么?业余时间?平面地图那该死的!”
As to what's so powerful about flatMap, well... First, it's the fundamental monadic operator. That means it is a common operation shared by, for example, containers (such as Option, collections, etc), continuations, state, etc. Second, while you can de-construct an Option, that, as opposed to flatMap, is not a monadic operation, so it cannot be as widely applied. Also, it requires too much knowledge about the data you are manipulating.
至于什么如此强大flatMap,嗯……首先,它是基本的一元运算符。这意味着它是一个通用操作,例如,容器(例如Option、集合等)、延续、状态等共享。第二,虽然您可以解构Option,但与 相反flatMap,它不是一元操作,所以不能广泛应用。此外,它需要太多关于您正在处理的数据的知识。
Note: previously I said matching was slower than flatMap-- the opposite is true as a matter of fact, up to the most recent version of Scala at the time of this writing, 2.10.1.)
注意:之前我说匹配比flatMap- 事实上恰恰相反,直到撰写本文时最新版本的 Scala,2.10.1。)
回答by Submonoid
The crucial thing about flatMapis that it's Scala's representation of the monadic bind operation. There are numerous tutorials on the web explaining the purpose of monads and why exactly they're so useful; James Iry has onewhich goes into some detail.
关键flatMap在于它是 Scala 对 monadic 绑定操作的表示。网络上有许多教程解释了 monad 的用途以及它们为何如此有用;James Iry 有一个详细介绍。
回答by Sean Parsons
Runar Bjarnason is the person you're looking for for the origin.
Runar Bjarnason 是您正在寻找起源的人。
Realising why it's so powerful is something that can only come with time to be honest. The Option class is the best place to start for seeing how you would repeatedly flatMap a series of lookups (for example) into a final result.
坦白说,只有随着时间的推移,才能意识到它为何如此强大。Option 类是了解如何将一系列查找(例如)重复 flatMap 到最终结果的最佳起点。

