158

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?

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
Christopher Edwards
  • 6,589
  • 8
  • 43
  • 57

16 Answers16

149

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.

Erik Forbes
  • 35,357
  • 27
  • 98
  • 122
  • 1
    That's only accurate when the lambda is being used as a filter. – Andru Luvisi Nov 07 '08 at 23:48
  • 4
    "Goes to" is a less awkward rendering of "populates the stack frame of" – Peter Wone Nov 08 '08 at 01:06
  • 1
    I really don't like "x becomes x * 2"; x is an input and remains the same throughout. Reading it that way sounds like an assignment operation. – Germán Nov 08 '08 at 01:46
  • @Peter - hah, very true. =) @Germán - I tend to agree, although I can't think of a better example for transformations. Maybe 'transforms to'? – Erik Forbes Nov 08 '08 at 04:55
  • 6
    I'd say "maps to" in that case. There's a slight risk then of people thinking you're talking about a container, but you can clear that up by explaining the first time you say it, that what you mean is "the lambda operator, you know, equals-sign-greater-than". – Steve Jessop Nov 08 '08 at 12:02
  • I've seen "goes to" in Adam Freeman's Visual C# 2010 and the 2008's LINQ in Action. I didn't care for it and decided to start pronouncing it as "for which". Considering Hejlsberg is the architect of C#, I'm glad he went that way too. – Kevin Scharnhorst Aug 07 '20 at 01:55
60

From MSDN:

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

Kent Boogaart
  • 175,602
  • 35
  • 392
  • 393
  • 8
    Intersting. Perhaps more interesting is that it was true for the [VS2010 version](http://msdn.microsoft.com/en-us/library/bb397687(v=vs.100).aspx), but the guidance has been removed from the [VS2012 version](http://msdn.microsoft.com/en-us/library/bb397687(v=vs.110).aspx). – Blair Conrad Oct 05 '13 at 20:47
  • 1
    @BlairConrad - maybe true when you wrote your comment, but it's there for all current VS versions now. – cp.engr Oct 06 '15 at 18:04
  • I no longer see the words "goes to" in the MSDN documentation linked in the above answer. I do remember around 2010 a mentor telling me that the lambda function should be read as "goes to" and that's how I have always read it. Stumbling onto this question however, reading it as "such that" makes more sense to me. – mfcallahan Oct 04 '18 at 22:33
19

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"

Gulzar Nazim
  • 51,744
  • 26
  • 128
  • 170
15

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

"p wang age of p greater than 16"

Aidos
  • 2,753
  • 3
  • 27
  • 31
7

I've seen people say, "Arrow."

Brian
  • 25,523
  • 18
  • 82
  • 173
7

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

CodeChef
  • 906
  • 1
  • 7
  • 21
5

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

Max Strini
  • 2,288
  • 21
  • 24
3

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

Will Vousden
  • 32,488
  • 9
  • 84
  • 95
2

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.

Jonesopolis
  • 25,034
  • 12
  • 68
  • 112
2

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.

Peter Wone
  • 17,965
  • 12
  • 82
  • 134
  • 1
    I don't see exactly how your conclusion follows from your discussion. On the other hand, I think characterizing => in terms of how closures might (or might not) be implemented is not a good way to come to grips with the question that is asked. – orcmid Nov 08 '08 at 22:42
  • The first paragraph is misleading. Lambda expressions are first class constructs. Function inlining is an optimization. The last paragraph is even more so. The bound variables in a lambda expressions (what you call "captured variables"), are not passed anywhere. They're boxed and added as a part of the lambda object. If they were passed, you'd have to name them again - to mention one difference. – Neowizard Nov 23 '16 at 09:45
2

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"
Mark Brackett
  • 84,552
  • 17
  • 108
  • 152
2

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"

orcmid
  • 2,618
  • 19
  • 20
  • 1
    ''I wouldn't use "such that" because that implies that the right hand side is a predicate'' - when it's being used as a predicate, then this is a valid translation, unless I'm misunderstanding you. – Erik Forbes Nov 11 '08 at 20:17
1

In Ruby, this same sybmol is called "hashrocket," and I've heard C# programmers use that term too (even though doing so is wrong).

Pharylon
  • 9,796
  • 3
  • 35
  • 59
1

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.

Tad Donaghe
  • 6,625
  • 1
  • 29
  • 64
0

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.

Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116
Allan
  • 11
0

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

Arthur
  • 1
  • 1