Some lambda expression can be reflected in the program "Context (context)" environment

Programming What is "Context (context)"?

 

Each section of the program has a lot of external variables. Add only like this simple function is not external variables. Once you have a program of external variables, this process is not complete, it can not operate independently. In order to make you run them, one by one, you should write some value to all external variables into account. The set of these values ​​is called a context.


Expression of lambda instance in C ++ which is, [here is written in the context] (int a, int b) {...}.

#include "stdafx.h"
#include <iostream>
int main()
{
    auto add = [](auto x) { return [x](auto y) {return x + y; }; };
    auto t = add(2);
    auto p = t(3);
    std::cout << p;
    return 0;
}

 

python lambda expression:

lambda expression can also be used in function def.

Look at an example:

Here the definition of an action function that returns a lambda expression. In which the lambda expression to get the value of the variable names x upper def scope.

The return value is a function of the action, a (22), a lambda expression is a call that is returned by the action.

Here you can also be written directly def lambda form. as follows

Guess you like

Origin www.cnblogs.com/leijiangtao/p/11958293.html