Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using lambdas to do nested functions

Tags:

c++

c++11

What's everybody's opinion on using lambdas to do nested functions in C++? For example, instead of this:

static void prepare_eggs()
{
   ...
}

static void prepare_ham()
{
   ...
}

static void prepare_cheese()
{
   ...
}

static fry_ingredients()
{
   ...
}

void make_omlette()
{
    prepare_eggs();
    prepare_ham();
    prepare_cheese();
    fry_ingredients();
}

You do this:

void make_omlette()
{
    auto prepare_eggs = [&]()
    {
       ...
    };

    auto prepare_ham = [&]()
    {
       ...
    };

    auto prepare_cheese = [&]()
    {
       ...
    };

    auto fry_ingredients = [&]()
    {
       ...
    };


    prepare_eggs();
    prepare_ham();
    prepare_cheese();
    fry_ingredients();
}

Having come from the generation that learned how to code by using Pascal, nested functions make perfect sense to me. However, this usage seemed to confuse some of the less experienced developers in my group at work during a code review where I made use of lambdas in this way.

like image 902
Arcadio Alivio Sincero Avatar asked Feb 24 '12 22:02

Arcadio Alivio Sincero


People also ask

Can lambda functions be nested?

A lambda function inside a lambda function is called a nested lambda function. Python allows lambda nesting, i.e., you can create another lambda function inside a pre-existing lambda function. For nesting lambdas, you will need to define two lambda functions – an outer and an inner lambda function.

Why lambda functions are not preferred as compared to normal functions?

lambda functions assigned to variable names have no advantage over def functions. lambda functions can be difficult or impossible to pickle.

When should lambdas be used?

Use a Lambda when you need to access several services or do custom processing. As data flows through services, you use Lambdas to run custom code on that data stream. This is useful in a Kinesis Pipeline that's receiving data from things like IoT devices.

How do you use lambda inside function?

When mydoubler is called and passed with equal parameter to lambda,Lambda function will be automatically called. Lambda function are the function which are defined without a name . There for it is also called anonymous function. In this code, myfunc doesn't return anything and mydoubler is not defined.


1 Answers

I don't see anything wrong with nested functions per se. I use lambdas for nested functions, but only if it meets some conditions:

  • It is called in more than once place. (otherwise just write the code directly if it's not too long)
  • It is really an internal function, so that that calling it in any other context would not make sense.
  • It's short enough (maybe 10 lines max).

So in your example I would not use lambdas for reason number one.

Conceptually nested functions can be useful for the same reason why private methods in classes are useful. They enforce encapsulation and they make it easier to see the structure of the program. If a function is an implementation detail to some other function then why not make it explicitly so?

The biggest problem I see is with readability; it's more difficult to read code that has a lot of nesting and indenting. Also, people are not very comfortable with lambdas yet so resistance is expected.

like image 137
Timo Avatar answered Sep 20 '22 18:09

Timo