views:

2118

answers:

13

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?

+61  A: 

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.

*Edit - added link to the original thread on the pre-release forums - thanks gmail!

Erik Forbes
That's only accurate when the lambda is being used as a filter.
Glomek
True - which is why I just edited the answer. =)
Erik Forbes
"Goes to" is a less awkward rendering of "populates the stack frame of"
Peter Wone
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
@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
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
'Maps to' works, yeah. I like that.
Erik Forbes
+1 for "maps to"
DrJokepu
Sorry for ruining your 6,666 rep :)
Anton Gogolev
Lol - no worries. =) It was pretty epic though.
Erik Forbes
+11  A: 

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
+4  A: 

I've seen people say, "Arrow."

Brian
I hope that's a joke.
Jacob Carpenter
It's not - I've seen the same.
Erik Forbes
I think people who say "arrow" mean this: ->
Dour High Arch
I've never seen that. Heard it, maybe, but not seen.
ctacke
Arrow seems reasonable to me as it meant to represent the arrow sign from lambda calculus. I’ve heard academic types call it arrow or right arrow, I’ve seen Haskell code on latex slides with the -> operator changed to an arrow symbol.
Robert
+4  A: 

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

CodeChef
+1  A: 

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.

Terry Donaghe
How do you say |? Same as pipe?
metanaito
Hard to find any guide on this, but _why uses "take each," i.e. People.each {|person| puts person.name} becomes "With People take each person and print name"
Adam Lassek
A: 

I'm a fan of Erik's answer. But, I've seen people in my department say the literal characters individually, "equals greater than" when telling another what to type in.

BoltBait
I get that, though it doesn't convey the meaning of the code - I think in the case of reading such an expression out to a fellow C# programmer, I'd use the word 'lambda' for the lambda operator.
Erik Forbes
Funny you should say that because the reason I even responded to this question... earlier today I heard one programmer say "lambda" and the other say "What?" so the original said "equals greater than!"
BoltBait
A: 

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.

Peter Wone
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
+1  A: 

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
+16  A: 

From MSDN:

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

HTH, Kent

Kent Boogaart
+2  A: 

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
''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
+4  A: 

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

"p wang age of p greater than 16"

Aidos
+3  A: 

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
+1  A: 

"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