Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Very simple explanation of a Lambda Expression

Tags:

c#

lambda

I am looking for a very simple - basic - no hardcore programming mumbo jumbo, simply put a generalized overview of a Lambda Expression in layman's terms.

like image 526
dennis Avatar asked May 14 '11 11:05

dennis


People also ask

What is lambda in simple terms?

AWS Lambda is a serverless compute service that runs your code in response to events and automatically manages the underlying compute resources for you. These events may include changes in state or an update, such as a user placing an item in a shopping cart on an ecommerce website.

What is the point of a lambda expression?

Lambda expression is an anonymous function that provides a very concise and functional syntax which is further used for writing anonymous methods. The programming of a function is a body concept, and it's used for creating expression tree types or delegates.

What is a lambda expression example?

Lambda expressions basically express instances of functional interfaces (An interface with single abstract method is called functional interface. An example is java.lang.Runnable). lambda expressions implement the only abstract function and therefore implement functional interfaces.

How do you read lambda expressions?

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.


4 Answers

A lambda expression is, simply put, a re-useable expression which takes a number of arguments:

x => x + 1;

The above expression reads "for a given x, return x + 1".

In .NET, this is powerful, because it can be compiled into an anonymous delegate, a nameless function you can declare inline with your code and evaluate to get a value:

int number = 100;

Func<int, int> increment = x => x + 1;

number = increment(number); // Calls the delegate expression above.

However, the real power of a lambda expression is that it can be used to initialize an in-memory representation of the expression itself.

Expression<Func<int, int>> incrementExpression = x => x + 1;

This means that you can give that expression to something like LINQ to SQL and it can understand what the expression means, translating it into a SQL statement that has the same meaning. This is where lambdas are very different from normal methods and delegates, and normally where the confusion begins.

like image 87
Paul Turner Avatar answered Nov 14 '22 00:11

Paul Turner


Lambda Expressions are inline functions that have a different syntax to regular functions.

Example Lambda Expression for squaring a number.

 x => x * x
like image 28
mikek3332002 Avatar answered Nov 14 '22 00:11

mikek3332002


A small unnamed inline method. Is that basic enough for you? I'm not sure what you are looking for exactly.

You also said in "layman's" terms - I presume that you have some level of software development experience (so not a complete layman)

like image 40
Colin Mackay Avatar answered Nov 13 '22 23:11

Colin Mackay


In non functional programming languages expressions (that act on variables) perform calculations and they perform those calculations once.

Lambda expressions allow you to define (in an expression) via different syntax code that can work on a list and can conceptually be considered a function.


You could simplify this to say "They let you define functions in expressions".


Does not quite get to the "why". The why is the more interesting, in my opinion. Lambda expression allow for the manipulation of functions and partial functions.

like image 44
Hogan Avatar answered Nov 14 '22 00:11

Hogan