How do I pronounce "=>" as used in lambda expressions in .Net

C#.NetLambdaConventions

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

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

> 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"

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:

x => x * 2;

would read

> x maps to x times 2.

That does seem much closer to the actual intention of the code than 'becomes' for this case.

Solution 2 - C#

From MSDN:

> All lambda expressions use the lambda > operator =>, which is read as "goes > to".

Solution 3 - C#

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"

Solution 4 - C#

I've always called it the "wang operator" :-)

"p wang age of p greater than 16"

Solution 5 - C#

I've seen people say, "Arrow."

Solution 6 - C#

I use "goes to" because a LINQ book told me to :)

Solution 7 - 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).

Solution 8 - C#

"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.

Solution 9 - C#

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.

Solution 10 - C#

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...

// "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"

Solution 11 - C#

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"

Solution 12 - C#

If you imagine a lambda expression as the anonymous method that it is, "goes to" makes decent sense enough.

(n => n == String.Empty)

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.

Solution 13 - C#

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.

Solution 14 - 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).

Solution 15 - C#

My two cents:

s => s.Age > 12 && s.Age < 20

"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

delegate(Student s) { return s.Age > 12 && s.Age < 20; };

=> 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.

Solution 16 - C#

My term for => as applied to the examples shown result

'result = s => s.Age > 12 && s.Age < 20'

s where s.Age is greater than 12 and s.Age less than 20

'result = x => x * 2'

x where x is multiplied by 2

'result = c => c.City == "London"'

c where c.city is equivalent to "London"

'result = n => n == String.Empty'

n where n is an empty string

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChristopher EdwardsView Question on Stackoverflow
Solution 1 - C#Erik ForbesView Answer on Stackoverflow
Solution 2 - C#Kent BoogaartView Answer on Stackoverflow
Solution 3 - C#Gulzar NazimView Answer on Stackoverflow
Solution 4 - C#AidosView Answer on Stackoverflow
Solution 5 - C#BrianView Answer on Stackoverflow
Solution 6 - C#CodeChefView Answer on Stackoverflow
Solution 7 - C#Max StriniView Answer on Stackoverflow
Solution 8 - C#Will VousdenView Answer on Stackoverflow
Solution 9 - C#Peter WoneView Answer on Stackoverflow
Solution 10 - C#Mark BrackettView Answer on Stackoverflow
Solution 11 - C#orcmidView Answer on Stackoverflow
Solution 12 - C#JonesopolisView Answer on Stackoverflow
Solution 13 - C#Tad DonagheView Answer on Stackoverflow
Solution 14 - C#PharylonView Answer on Stackoverflow
Solution 15 - C#AllanView Answer on Stackoverflow
Solution 16 - C#ArthurView Answer on Stackoverflow