Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to translate a lambda expression into English? [closed]

Tags:

c#

vb.net

lambda

I'm a VB.NET programmer who also knows some C#. I've recently come across lambda expressions which in VB can look like this:

a = Function() b + c

However in C# they look like this:

a => b + c;

(Which, to me, looks a lot like a bizarre pointer expression from C)

The problem is that when reading C# code I mentally parse the operators into english words. So a = b + c; becomes "add b and c and assign to a". However I have no mental translation in my built in parser for the for the => operator yet so a => b + c; becomes either "a equals pointer to b and c" which makes no sense or "a is a lambda of b and c" which is dreadful.

I'm sure that the language creators meant for that operator to stand for a concept and that concept should be able to be reduced to a single word or very short phrase.

So what is the intended English translation for the => operator?

p.s. Note that I've tried to make the examples to be as simple as possible so as not to get bogged down in the specifics.

like image 449
Robin G Brown Avatar asked Apr 22 '14 09:04

Robin G Brown


People also ask

How do you read a lambda expression?

All lambda expressions use the lambda operator =>, which is read as "goes to". The left side of the lambda operator specifies the input parameters (if any) and the right side hold the expression or statement block. The lambda expression x => x * 2 is read "x goes to 2 times x." This reduced the no.

What is lambda expression in C# with example?

The expression num => num * 5 is a lambda expression. The => operator is called the "lambda operator". In this example, num is an input parameter to the anonymous function, and the return value of this function is num * 5 . So when multiplyByFive is called with a parameter of 7 , the result is 7 * 5 , or 35 .


2 Answers

You have your example slightly wrong.

This VB code

a = Function()
  Return b + c
End Function

looks like this in C#:

Func<int> a = () => b + c;

The problem with the suggestion of calling => "returns" is that this doesn't work with lambdas that don't return anything:

For example:

Action a = () => Console.WriteLine("Test");

If you call => "returns" in that scenario, it would be a bit confusing, I think.

Generally, the term used for => is "goes to" but some people say "maps to" or (in the case of lambdas that are used as predicates) "such that".

My personal preference is "maps to" (as suggested in the very first reply to the OP!).

like image 172
Matthew Watson Avatar answered Sep 17 '22 20:09

Matthew Watson


How about 'a returns b plus c'?

like image 39
Christofer Ohlsson Avatar answered Sep 19 '22 20:09

Christofer Ohlsson