Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Example of a Practical Use for Lambdas [duplicate]

Tags:

c++

lambda

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)?

like image 528
joe_04_04 Avatar asked Nov 06 '17 10:11

joe_04_04


People also ask

Can we duplicate lambda function?

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.

What do you use lambdas for?

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.


2 Answers

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.

like image 106
StoryTeller - Unslander Monica Avatar answered Sep 28 '22 07:09

StoryTeller - Unslander Monica


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 structs" 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.

like image 45
Vittorio Romeo Avatar answered Sep 28 '22 06:09

Vittorio Romeo