我如何发音“=>” 用于 .Net 中的 lambda 表达式
我很少见到其他程序员!
当我第一次看到这个标记时,我的想法是“暗示”,因为这就是它在数学证明中的含义,但这显然不是它的意义。
那么我该如何说或读“=>” 例如:-
IEnumerable<Person> Adults = people.Where(p => p.Age > 16)
或者是否有一种一致同意的表达方式?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
在阅读该运算符时,我通常会说“这样”。
在你的例子中,p => p.年龄> 16 读作“P,使得 p.Age 大于 16”。
事实上,我在 linq 预发布官方论坛上问了这个问题,Anders Hejlsberg 回应说
至于“去”——这对我来说从来没有意义。 “p”不会去任何地方。
在通过电话向某人阅读代码的情况下,只要他们是 C# 程序员,我就会使用“lambda”这个词 - 也就是说,“p lambda p 点年龄大于十六。”
Steve Jessop 在评论中提到了在转换的情况下“映射到” - 所以以 Anders 的例子为例:
会读
对于本例来说,这似乎比“成为”更接近代码的实际意图。
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."
In fact, I asked this very question on the official linq pre-release forums, and Anders Hejlsberg responded by saying
As far as 'goes to' - that's never made sense to me. 'p' isn't going anywhere.
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."
In comments Steve Jessop mentioned 'maps to' in the case of transformations - so taking Anders' example:
would read
That does seem much closer to the actual intention of the code than 'becomes' for this case.
来自 MSDN:
From MSDN:
通过电话阅读代码
来自 Eric Lippert:
我个人会说 c=>c+1 为“看就看加一”。 我听说过的一些变化:
对于投影, (Customer c)=>c.Name: "customer see gets see dot name"
对于谓词, (Customer c)=>c.Age > 21:“顾客看到这样的看到点年龄大于二十一”
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:
For a projection, (Customer c)=>c.Name: "customer see becomes see dot name"
For a predicate, (Customer c)=>c.Age > 21: "customer see such that see dot age is greater than twenty-one"
我一直称其为“wang 运算符”:-)
“p wang 年龄大于 16”
I've always called it the "wang operator" :-)
"p wang age of p greater than 16"
我见过有人说“箭头”。
I've seen people say, "Arrow."
我使用“goes to”是因为一本 LINQ 书告诉我这样做:)
I use "goes to" because a LINQ book told me to :)
“映射到”怎么样? 它比其他替代方案。 不过,如果 MSDN 页面似乎暗示已经有一个标准,那么也许您应该遵循该标准(至少对于 C# 代码而言)。
How about "maps to"? It's both succinct and arguably more technically accurate (i.e. no suggestion of a state change as with "goes to" or "becomes", no conflation of a set with its characteristic function as with "such that" or "for which") than the other alternatives. Though if there's already a standard as the MSDN page appears to imply, maybe you should just go with that (at least for C# code).
“映射到”是我更喜欢的发音。 从数学上讲,函数将其参数“映射”到其返回值(甚至可以将该函数称为“映射”),因此对我来说,在编程中使用这一术语是有意义的,特别是作为函数式编程(尤其是 lambda 演算)与数学非常接近。 它也比“成为”、“去”等更加中立,因为它并不暗示状态的改变,正如上下文无关提到的那样。
"Maps to" is my preferred pronunciation. Mathematically speaking, a function "maps" its arguments to its return value (one might even call the function a "mapping"), so it makes sense to me to use this terminology in programming, particularly as functional programming (especially the lambda calculus) is very close to mathematics. It's also more neutral than "becomes", "goes to", etc., since it doesn't suggest change of state, as contextfree mentioned.
我没有多想,只是简洁地说了一个“到”。 它简短明了,并且意味着变量被传递给表达式。 我想它可能会与数字 2(“二”)混淆,但我说话时倾向于将“to”发音更像“ta”。 没有人(至少知道 lambda 的人)告诉我他们认为它模棱两可......
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 to the 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...
我的简短回答是:“c 'lambda-of' e”。 虽然我坚持“'lambda' c 'function' e”,但我认为 lambda-of 是普世的妥协。 分析如下。
如果只是为了奇怪的答案,这是一个很好的问题。 大多数翻译除了 lambda 表达式之外还有其他含义,从而导致奇怪的解释。 作为一名老 lambda 表达式黑客,我只是忽略 .NET 符号并将其在我的脑海中重写为 lambda,同时希望他们为此做了几乎任何其他事情。
对于通过电话叙述代码,您希望有人能够按顺序写下代码。 当然,这是一个问题,但 lambda-arrow 或其他东西可能是你能得到的最好的,或者也许是 lambda-in,但 lambda-of 是最准确的。
问题是中缀的用法以及如何命名整个事物以及左右部分的角色以及在中缀位置说出时起作用的东西。
这可能是一个过度约束的问题!
我不会使用“这样”,因为这意味着右侧是左侧应该满足的谓词。 这与谈论右侧非常不同,左侧已被抽象为函数参数。 (MSDN 关于“所有 lambda 表达式”的声明不仅令人反感而且不准确。)
“goes to”虽然可能是我们所能得到的最接近的结果,但有些令人恼火的地方。 “Goes to”意味着一个转换,但并不存在某个变量 c 会转到 c 中的表达式。 函数的抽象有点难以捉摸。 我可以习惯这一点,但我仍然渴望一些强调变量抽象的东西。
由于在到目前为止使用的情况下,左侧始终是一个简单的标识符[但等待稍后可能会混淆的扩展],我认为对于“c =>表达式”我会读“c'lambda-function'表达式”' 甚至“c 'arg' 'function' 表达式”。 在最后一种情况下,我可以说“b'arg'c'arg''function'表达式”之类的东西。
最好更清楚地表明正在引入 lambda 表达式,并说出类似“'arg' b 'arg' c 'function' 表达式”之类的内容。
弄清楚如何将所有这些内容翻译成其他语言对于学生来说是一项练习[;<)。
我仍然担心“(b, c) =>表达式”和其他可能出现的变体(如果它们还没有出现的话)。 也许是“'args' b, c '函数'表达式”。
经过所有这些思考后,我注意到我正在将“c => e”翻译为“'lambda' c 'function' e”,并注意到到精确形式的映射是通过上下文来理解的: λc( e),c=> e, f where f(c) = e 等。
我希望“goes-to”解释会占上风,因为这是大多数人第一次看到 lambda 表达式的地方。 太可惜了。 一个好的折衷方案可能是“c 'lambda-of' e”
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.
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.
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.
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.)
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.
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".
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".
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".
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 where f(c) = e, etc.
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 表达式想象为匿名方法,那么“goes to”就足够有意义了。
n “转到”表达式 n == String.Empty。
它转到匿名方法,因此您不必转到代码中的方法!
对此感到抱歉。
老实说,我不喜欢在脑海中使用“goes to”,但我看到其他人说这看起来很奇怪,我想我会澄清这一点。
If you imagine a lambda expression as the anonymous method that it is, "goes to" makes decent sense enough.
n "goes to" the expression n == String.Empty.
It goes to the anonymous method, so you don't have to go to the method in the code!
Sorry for that.
Honestly, I don't like to use "goes to" in my head, but I saw other people saying it seemed weird, and I thought I'd clear that up.
除了获取前面的作用域(在 lambda 表达式出现的位置处正常代码行作用域内的所有变量和常量都可用于该表达式的代码)之外,lambda 表达式本质上是内联函数的语法糖。
生产运算符(“=>”)左侧的值列表提供了用于调用此函数的堆栈帧的结构和内容。 您可以说值列表贡献了参数声明和传递的参数; 在更传统的代码中,它们确定用于调用函数的堆栈帧的结构和内容。
结果,值“转到”表达式代码。 您愿意说“定义堆栈帧”还是“转到”? :)
在用作过滤条件的布尔表达式的狭义应用中(此问题的其他答案广泛考虑的 lambda 表达式的主要用途),跳过该方法以支持代码的意图是非常合理的,这会导致“for which”同样简洁,更多地说明了代码的含义。
然而,lambda 表达式并不是 Linq 的唯一领域,在这种情况之外,应该使用更通用的形式“goes to”。
但为什么要“去”呢?
因为“填充以下代码的堆栈帧”太长,无法继续说下去。 我想你可以说“被传递给”。
显式传递的参数和捕获的变量之间的一个关键区别(如果我没记错的话,请纠正我,如果我错了)是前者通过引用传递,后者通过值传递。
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.
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.
However, lambda expressions are not the sole province of Linq and outside of this context the more general form "goes to" should be used.
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.
部分问题在于,根据其结构的不同,您可以以不同的方式大声朗读它。 遗憾的是它不像 ruby 的 | 那样漂亮或集成。
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 中,这个 sybmol 被称为“hashrocket”,我听说 C# 程序员也使用这个术语(尽管这样做是错误的)。
In Ruby, this same sybmol is called "hashrocket," and I've heard C# programmers use that term too (even though doing so is wrong).
我的两分钱:
“带有参数 s 的 Lambda 表达式是 {
return s.Age > 12 && s.Age < 20;
}”我喜欢这个,因为它让我想起了 lamdba 表达式来自
=> 只是一个快捷方式,因此您不必使用 delegate 关键字并包含类型信息,因为编译器可以推断它。
My two cents:
"Lambda Expression with parameter s is {
return s.Age > 12 && s.Age < 20;
}"I like this because it reminds me of where lamdba expression comes from
=> is just a shortcut so you don't have to use the delegate keyword and include the type info since it can be inferred by the compiler.
我的术语=> 应用于显示结果
s 的示例,其中 s.Age 大于 12,s.Age 小于 20
x,其中 x 乘以 2
c,其中 c.city 相当于“London”
n,其中 n 是空字符串
My term for => as applied to the examples shown result
s where s.Age is greater than 12 and s.Age less than 20
x where x is multiplied by 2
c where c.city is equivalent to "London"
n where n is an empty string