C# 我如何发音 .Net 中 lambda 表达式中使用的“=>”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/274022/
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
How do I pronounce "=>" as used in lambda expressions in .Net
提问by Christopher Edwards
I very rarely meet any other programmers!
我很少遇到其他程序员!
My thought when I first saw the token was "implies that" since that's what it would read it as in a mathematical proof but that clearly isn't its sense.
当我第一次看到这个标记时,我的想法是“暗示”,因为这就是它在数学证明中的意思,但这显然不是它的意义。
So how do I say or read "=>" as in:-
那么我如何说或读“=>”,如:-
IEnumerable<Person> Adults = people.Where(p => p.Age > 16)
Or is there even an agreed way of saying it?
或者甚至有一种商定的表达方式?
采纳答案by Erik Forbes
I usually say 'such that' when reading that operator.
在阅读该运算符时,我通常会说“这样”。
In your example, p => p.Age > 16 reads as "P, such that p.Age is greater than 16."
在您的示例中, p => p.Age > 16 读作“P,使得 p.Age 大于 16”。
In fact, I asked this very question on the official linq pre-release forums, and Anders Hejlsberg responded by saying
事实上,我在官方 linq 预发布论坛上问了这个问题,Anders Hejlsberg 回答说
I usually read the => operator as "becomes" or "for which". For example,
Func f = x => x * 2;
Func test = c => c.City == "London";
reads as "x becomes x * 2" and "c for which c.City equals London"
我通常将 => 操作符读作“becomes”或“for which”。例如,
Func f = x => x * 2;
Func test = c => c.City == "London";
读作“x 变为 x * 2”和“c.City 等于伦敦的 c”
As far as 'goes to' - that's never made sense to me. 'p' isn't going anywhere.
至于“去”——这对我来说从来没有意义。“p”不会去任何地方。
In the case of reading code to someone, say, over the phone, then as long as they're a fellow C# programmer, I'd just use the word 'lambda' - that is, "p lambda p dot age greater-than sixteen."
在给某人阅读代码的情况下,比如说,通过电话,那么只要他们是 C# 程序员,我就会使用“lambda”这个词——也就是说,“p lambda p dot age >十六。”
In comments Steve Jessop mentioned 'maps to' in the case of transformations - so taking Anders' example:
在评论中,Steve Jessop 在转换的情况下提到了“映射到”——所以以安德斯的例子为例:
x => x * 2;
would read
会读
x maps to x times 2.
x 映射到 x 乘以 2。
That does seem much closer to the actual intention of the code than 'becomes' for this case.
在这种情况下,这似乎比“变成”更接近代码的实际意图。
回答by Gulzar Nazim
Reading Code Over the Telephone
From Eric Lippert:
埃里克·利珀特:
I personally would say c=>c+1 as "see goes to see plus one". Some variations that I've heard:
我个人会说 c=>c+1 为“见即见加一”。我听到的一些变化:
For a projection, (Customer c)=>c.Name: "customer see becomes see dot name"
对于投影,(Customer c)=>c.Name: "customer see become see dot name"
For a predicate, (Customer c)=>c.Age > 21: "customer see such that see dot age is greater than twenty-one"
对于谓词,(Customer c)=>c.Age > 21:“客户看到这样的看到点年龄大于二十一”
回答by Brian
I've seen people say, "Arrow."
我见过人们说,“箭头”。
回答by CodeChef
I use "goes to" because a LINQ book told me to :)
我使用“goes to”是因为一本 LINQ 书告诉我:)
回答by Tad Donaghe
Part of the problem is that you can read it out loud differently depending on how it's structured. It's a shame it's not as pretty or as integrated as ruby's |'s.
部分问题在于,您可以根据它的结构以不同的方式大声朗读它。遗憾的是它不像 ruby 的 | 那样漂亮或集成。
回答by Peter Wone
Apart from acquiring the preceding scope (all variables and constants that are in scope for a normal line of code at the point where a lambda expression occurs are available to the code of the expression) a lambda expression is essentially syntactic sugar for an inline function.
除了获得前面的作用域(在 lambda 表达式出现的地方,在正常代码行的作用域内的所有变量和常量都可用于表达式的代码),一个 lambda 表达式本质上是内联函数的语法糖。
The list of values to the left of the production operator ("=>") contributes the structure and content of the stack frame used to make the call to this function. You could say that the list of values contributes both the parameter declarations and the arguments that are passed; in more conventional code these determine the structure and content of the stack frame used to make the call to a function.
产生式运算符(“=>”)左侧的值列表提供了用于调用此函数的堆栈帧的结构和内容。您可以说值列表对参数声明和传递的参数都有贡献;在更传统的代码中,这些决定了用于调用函数的堆栈帧的结构和内容。
As a result, the values "go to" the expression code. Would you rather say "defines the stack frame for" or "goes to" ? :)
结果,值“转到”表达式代码。你更愿意说“定义堆栈框架”还是“转到”?:)
In the narrowly defined application of boolean expressions used as filter conditions (a dominant use of lambda expressions extensively considered by other answers to this question) it is very reasonable to skip the method in favour of the intent of the code, and this leads to "for which" being just as succinct and saying more about the meaning of the code.
在用作过滤条件的布尔表达式的狭义应用中(该问题的其他答案广泛考虑使用 lambda 表达式的主要用途),跳过该方法以支持代码的意图是非常合理的,这导致“ for which”同样简洁,并且更多地说明了代码的含义。
However, lambda expressions are not the sole province of Linq and outside of this context the more general form "goes to" should be used.
但是,lambda 表达式并不是 Linq 的唯一领域,在此上下文之外,应该使用更通用的“goes to”形式。
But why "goes to" ?
但为什么“去”呢?
Because "populates the stack frame of the following code" is far too long to keep saying it. I suppose you could say "is/are passed to".
因为“填充以下代码的堆栈帧”太长了,无法继续说下去。我想你可以说“是/被传递给”。
A crucial difference between explicitly passed parameters and captured variables (if I remember correctly - correct me if I'm wrong) is that the former are passed by reference and the latter by value.
显式传递的参数和捕获的变量之间的一个重要区别(如果我没记错的话 - 如果我错了,请纠正我)是前者是通过引用传递的,而后者是通过值传递的。
回答by Mark Brackett
I haven't thought it about much, but I just succintly say "to". It's short and concise, and implies that the variable is passed tothe expression. I suppose it could be confused with the numeral 2 ("two"), but I tend to pronounce "to" more like "ta" when speaking. Nobody (who knows lambdas, at least) has ever told me they thought it ambiguous...
我没有想太多,只是简单地说了“对”。它简短而简洁,并暗示将变量传递给表达式。我想它可能会与数字 2(“二”)混淆,但我在说话时倾向于将“to”发音更像“ta”。没有人(至少知道 lambdas 的人)告诉我他们认为它不明确......
// "Func f equals x to x times two"
Func f = x=> x * 2;
// "Func test equals c to c dot City equals London"
Func test = c => c.City == "London"
回答by Kent Boogaart
回答by orcmid
My short answer: "c 'lambda-of' e". Although I am clinging to "'lambda' c 'function' e", I think lambda-of is the ecumenical compromise. Analysis follows.
我的简短回答:“c 'lambda-of' e”。虽然我坚持“'lambda' c 'function' e”,但我认为 lambda-of 是普世妥协。分析如下。
This is a great question if only for the bizarre answers. Most of the translations have other meanings than for lambda expressions, leading to exotic interpretations. As an old lambda-expression hacker, I just ignore the .NET notation and rewrite it as lambda in my head while wishing they had done almost anything else for this.
如果只是为了奇怪的答案,这是一个很好的问题。大多数翻译具有除 lambda 表达式之外的其他含义,从而导致异国情调的解释。作为一个老 lambda 表达式黑客,我只是忽略了 .NET 表示法并在我的脑海中将它重写为 lambda,同时希望他们为此做了几乎任何其他事情。
For narrating code over the phone, you want someone to be able to write the code down in sequence. That is a problem, of course, but lambda-arrow or something is probably the best you can get, or maybe lambda-in, but lambda-of is the most accurate.
对于通过电话叙述代码,您希望有人能够按顺序写下代码。当然,这是一个问题,但 lambda-arrow 或其他东西可能是你能得到的最好的,或者 lambda-in,但 lambda-of 是最准确的。
The problem is the infix usage and how to name the whole thing and the role of the left and right parts with something that works when spoken in the infix place.
问题是中缀用法以及如何命名整个事物以及左右部分的作用,这些东西在中缀位置说话时有效。
This may be an over-constrained problem!
这可能是一个过度约束的问题!
I wouldn't use "such that" because that implies that the right hand side is a predicate that the left-hand side should satisfy. That is very different from talking about a right-hand side from which the left-hand side has been abstracted as a functional parameter. (The MSDN statement about "All lambda expressions" is simply offensive as well as inaccurate.)
我不会使用“这样的”,因为这意味着右侧是左侧应该满足的谓词。这与将左侧抽象为功能参数的右侧非常不同。(有关“所有 lambda 表达式”的 MSDN 声明简直令人反感且不准确。)
Something rankles about "goes to" although it may be as close as we can get. "Goes to" implies a transformation, but there is not exactly some variable c that goes to an expression in c. The abstraction to a function is a little elusive. I could get accustomed to this, but I still yearn for something that emphasizes the abstraction of the variable.
尽管它可能尽可能接近,但对“去”有些不满。“Goes to”意味着一个转换,但并不完全有某个变量 c 会转到 c 中的表达式。对函数的抽象有点难以捉摸。我可以习惯这一点,但我仍然渴望一些强调变量抽象的东西。
Since the left-hand side is always a simple identifier in the cases used so far [but wait for extensions that may confuse this later on], I think for "c => expression" I would read "c 'lambda-function' expression"' or even "c 'arg' 'function' expression". In the last case, I could then say things like "b 'arg' c 'arg' 'function' expression".
由于在迄今为止使用的情况下,左侧始终是一个简单的标识符[但请等待稍后可能会混淆的扩展名],我认为对于“c => 表达式”,我会阅读“c 'lambda-function' 表达式"' 甚至 "c 'arg' 'function' 表达式"。在最后一种情况下,我可以说诸如“b 'arg' c 'arg' 'function' 表达式”之类的东西。
It might be better to make it even more clear that a lambda-expression is being introduced and say something like "'arg' b 'arg' c 'function' expression".
最好更清楚地说明正在引入 lambda 表达式并说诸如“'arg' b 'arg' c 'function' 表达式”之类的内容。
Figuring out how to translate all of this to other languages is an exercise for the student [;<).
弄清楚如何将所有这些翻译成其他语言是学生的练习 [;<)。
I still worry about "(b, c) => expression" and other variants that may come into being if they haven't already. Perhaps "'args' b, c 'function' expression".
我仍然担心“(b, c) => 表达式”和其他可能出现的变体,如果它们还没有出现的话。也许“'args' b, c 'function' 表达式”。
After all of this musing, I notice that I am coming around to translating "c => e" as "'lambda' c 'function' e" and noticing that the mapping to exact form is to be understood by context: λc(e), c => e, f wheref(c) = e, etc.
在所有这些沉思之后,我注意到我正在将“c => e”翻译为“'lambda' c 'function' e”,并注意到映射到确切形式是通过上下文来理解的:λc(e ), c => e, f其中f(c) = e 等。
I expect that the "goes-to" explanation will prevail simply because this is where a dominant majority is going to see lambda expressions for the first time. That's a pity. A good compromise might be "c 'lambda-of' e"
我希望“首选”解释会占上风,因为这是大多数人第一次看到 lambda 表达式的地方。太可惜了。一个好的折衷方案可能是“c ' lambda-of' e”
回答by Aidos
I've always called it the "wang operator" :-)
我一直称它为“王操作员”:-)
"p wang age of p greater than 16"
“p wang 年龄大于 16”