Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the advantage of "lambda expressions"?

Tags:

c++

syntax

lambda

The reason for lambda expressions is implicitly generate function objects in a "more convenient way". As you can see from the example below, it is not only less convenient and longer, but also has a confusing syntax and notation.

Are there any uses of this, where it actually makes code more readable?

cout << count(vec, [&](int a){ return a < x; }) << endl;  // lambda
cout << count(vec, Less_than<int> (x)) << endl;           // normal functor def-n
like image 872
Oleksiy Avatar asked Aug 11 '13 02:08

Oleksiy


People also ask

What is the advantage of lambda expression in Python?

The lambda keyword in Python provides a shortcut for declaring small anonymous functions. Lambda functions behave just like regular functions declared with the def keyword. They can be used whenever function objects are required.

What is the advantage of lambda expression in C++?

One of the new features introduced in Modern C++ starting from C++11 is Lambda Expression. It is a convenient way to define an anonymous function object or functor. It is convenient because we can define it locally where we want to call it or pass it to a function as an argument.

What are the advantages of lambda expression over anonymous methods?

Concise code, more readability, less ceremony to do simple things i.e. replacement of anonymous class (you still have to write an anonymous class, in case of lambda you don't have to write a class). Reuse of code, create lambda expressions and pass it around methods.

What is the purpose of a lambda expression?

Lambda expressions are a new and important feature included in Java SE 8. They provide a clear and concise way to represent one method interface using an expression. Lambda expressions also improve the Collection libraries making it easier to iterate through, filter, and extract data from a Collection .


2 Answers

It is more readable because it places the implementation of the function right into the spot where it is called, thus keeping the natural top-to-bottom flow of code unchanged.

You see, ordinary functions have their pros and cons. On the one hand, functions help reduce repetitions in the code, this making the code more structured and more readable. On the other hand, functions break the natural flow of code and transfer control to a completely different location. This can reduce the readability for rather obvious reasons: it is like reading a book that is riddled with nested forward and backward references.

So, in order to properly take advantage of the properties of ordinary functions, one should use them to implement well-thought-through, complete and isolated abstractions. That way ordinary functions will improve the readability of the code.

But for small "disposable" one-time-use utility code, ordinary functions don't work so well. They can actually makes the code significantly less readable. This is where lambda functions come in. They allow one to inject that disposable utility code straight into the point of the call, where it is necessary.

like image 185
AnT Avatar answered Oct 03 '22 15:10

AnT


The lambda expression exists to simplify code. This:

auto fun = []() { return; };

Is replaced by the compiler with:

// Namespace scope
struct __lambda_1 {
    void operator()() { return; }
};

// local scope
__lambda_1 fun{};

This is the primary motivation for the lambda syntax: To replace traditional function objects with an easier to read anonymous function declared at the site where it is needed, rather than having a separate function object that must be declared in another scope. It is not merely to replace named function objects.

Indeed, the standard library includes a number of named function objects like std::unary_function and things like std::less. But these objects have limited utility and cannot take on every potential role that a lambda would.

So, yes, it can make code substantially more readable, by placing code that the standard library doesn't provide precisely where it's required, without polluting your code with dozen-line structs and polluting your namespace with names you'll likely use no more than once or twice.

like image 43
greyfade Avatar answered Oct 03 '22 16:10

greyfade