What is Lambda expression and how does it work as anonymous?

Lambda expression

The term Lambda expression has derived its name from lambda calculus which in turn is a mathematical notation applied for defining functions. In computer programming, a lambda expression is an anonymous function and it is mostly used to create delegates in LINQ. It is a method without a declaration that is not bound to an identifier. Lambda functions can have any number of arguments but only one expression. The expression is evaluated and returned. It can be used wherever function objects are required. If the function is only used once, or a limited number of times, an anonymous function may be syntactically lighter than using a named function. This isn't exactly the same as lambda in functional programming languages, but it is a very powerful concept.

The canonical example is that you'll pass a comparison function to a generic "sort" routine, and instead of going to the trouble of defining a whole function to describe this comparison, you can just pass a lambda expression describing the comparison. However, this misses one of the most important features of Lambda Expressions, which is that they execute in the context of their appearance. Therefore, they can use the values of the variables that are defined in that context. This differentiates function-pointers from true lambda expressions.

A lambda expression within a query operator is evaluated by the same upon demand and continually works on each of the elements in the input sequence and not the whole sequence. Developers are allowed by Lambda expression to feed their own logic into the standard query operators.

There are some rules while using variable scope in a lambda expression like variables that are initiated within a lambda expression are not meant to be visible in an outer method. There is also a rule that a captured variable is not to be garbage collected unless the delegate referencing the same becomes eligible for the act of garbage collection. Moreover, there is a rule that prohibits a return statement within a lambda expression to cause return of an enclosing method.

Lambda expressions are used in Expression Tree construction extensively. An expression tree give away code in a data structure resembling a tree in which every node is itself an expression like a method call or can be a binary operation like x.

Depending on the language, anonymous functions are often implemented differently than named functions. In languages supporting mutable variables, proper lambda expressions offer the power to change the values of those variables. Lambda expressions appear in all LISPs, Perl, Python, and sufficiently recent versions of C++, Objective C, C# and Java 8, but notably not in C even though it has a way to deal with passing functions around as parameters. They are a syntax element with particular semantics, and those semantics place more requirements on the runtime than C was designed to require.

In Python, we generally use it as an argument to a higher-order function. Lambda functions are used along with built-in functions like filter(), map() etc.

double = lambda x: x * 2

In the above program, lambda x: x * 2 is the lambda function. Here x is the argument and x * 2 is the expression that gets evaluated and returned. This function has no name. It returns a function object which is assigned to the identifier double.

In C#, type inference is used conveniently in a variety of situations and that too without specifying the types explicitly. However in case of a lambda expression, type inference will work only when each type has been specified as the compiler must be satisfied.

delegate int Transformer (int i);

Here the compiler employ the type inference to draw upon the fact that x is an integer and this is done by examining the parameter type of the Transformer.

Recommended for you