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
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.
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.
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.
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 .
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.
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 struct
s and polluting your namespace with names you'll likely use no more than once or twice.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With