I’ve recently started the process of researching lambdas for personal use. I’ve been coding for a few years now, I’ve released multiple products into the wild, and yet, I’ve never found myself needing to use lambdas. I’ve read the other stack exchange answers for various lambda questions, but I haven’t found an explanation that shows a dead-simple example that really drives the idea that lambdas are necessary (for some situations). After doing a bit of research, I’m not convinced that some of the specific uses of lambdas can’t be implemented with standard functions, however, that doesn’t mean much because my scope of the topic is extremely limited.
Can someone provide a rather simple use case for lambdas that demonstrates how they can be more powerful than typical functions (in the correct situations)?
There is no provided function to copy/clone Lambda Functions and API Gateway configurations. You will need to create new a new function from scratch. If you envision having to duplicate functions in the future, it may be worthwhile to use AWS CloudFormation to create your Lambda Functions.
For example, you can use Lambda to: Build data-processing triggers for AWS services such as Amazon Simple Storage Service (Amazon S3) and Amazon DynamoDB. Process streaming data stored in Amazon Kinesis. Create your own backend that operates at AWS scale, performance, and security.
C++ was Turing complete long before C++11. Lambdas aren't there for making something impossible into a possibility. You can accomplish much without them. They are there to streamline certain pieces of code.
For instance, to allow on-the-fly creation of functors to the standard algorithm library. Sure you can define one on your own. But that requires going outside your funcion, and naming a new type. A whole lot of boilerplate for a simple call to std::transform
, as an example.
They keep code localized, don't require you to pollute any namesapces, and are easy to use once you get the hang of it.
As a bonus, in his talk about functional C++, Kevlin Henney reduces an example function from a framework into something that really cries out "use a lambda". So to borrow a little from his talk, consider a command registration function:
void register_command(std::string cmdName, std::function<void()> f);
You want to register a command that just turns on a light bulb. How much boilerplate would you need to write for a custom functor, compared to the following?
register_command("Turn On Light-bulb", [&light_controller]() {
light_controller.on();
});
Ponder that, and which code base you'd personally rather maintain.
Everything that can be done with lambdas nowadays could have been done in C++03 by handwriting a struct
/class
with an overloaded operator()
(commonly called "functors"). There was a huge issue with that: lack of terseness and lack of locality.
Imagine passing a templated predicate to something like std::find_if
in C++03:
struct predicate
{
template <typename T>
bool operator()(const T& x) { return boop(x); }
};
void foo()
{
something(std::find_if(c.begin(), c.end(), predicate()));
}
You need to:
Define predicate
somewhere, potentially far away from where it's used.
Have boilerplate for the struct
and the operator()
overload.
Jump around in the source code while reading it, to understand what predicate
is doing.
Compare that to a C++14 generic lambda:
void foo()
{
something(std::find_if(c.begin(), c.end(),
[](const auto& x){ return boop(x); });
}
Boom. Everything is there, local, with minimal boilerplate.
Lambdas are not magic. They purely are syntactic sugar. But I'm my opinion, they're "syntactic sugar to handwritten struct
s" as "C++ is syntactic sugar to C".
They change the way you write code, they make functional paradigms (e.g. higher-order functions) viable, and can make your code more powerful and safer.
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