Lambda expressions in C Language

Source: Internet
Author: User

Lambda expressions in C Language
Preface

I would like to give a few comments. My friends and colleagues all like to record some of their questions and experiences on learning computer programming technology on their blogs. Personally, this is a good habit, especially in the computer programming field. There are a wide range of technical directions and there is no end to it. So I also started my first blog in my life. I hope my friends can encourage me and discuss it more. Thank you for that. The more advanced the computing technology is, the more deeply it is, the more it is connected to mathematics. I want to reach the level of greatness. Well, I am self-aware. Although there are many computer technologies, we do not need to be proficient in every technology in every field. Of course, if we can do it, I sincerely admire and envy it. It is not easy for us to focus on some technical fields and achieve the word "proficient". After all, the mental and physical losses are too high, it is difficult for ordinary people to achieve all-encompassing results. I personally like the C language. I have been very keen on it for more than six years. I learned other languages from time to time and used the C language to imitate their interesting technologies. Let's take a look at using C language to implement Lambda expressions.

What is a Lambda expression?

"Lambda Expression" is an anonymous function. A Lambda Expression is named after the Lambda Algorithm in mathematics and corresponds directly to the lambda abstraction action. It is an anonymous function, that is, a function without a function name. Lambda expressions can represent closures (note that they are different from traditional mathematics ).

C-Lambda expressions

Lambda expressions are implemented in C language. Generally, they are defined by macros. In this way, you can use a form similar to Lambda (...). See the following:

# Define cgs_lambda (return_type, function_body )\

({Return_type cgs_lambda_func function_body cgs_lambda_func ;})

The above is the macro definition of Lambda expressions. The "cgs _" prefix is used in my own projects, not the focus. (Return_type, function_body) is used in ({return_type cgs_lambda_func function_body cgs_lambda_func;}) Statements, return_type is the return type, function_body is the anonymous function body in the Lambda expression. Note: cgs_lambda_func in the middle is actually an anonymous function name. The function address of cgs_lambda_func is defined here. How to Use cgs_lambda expressions is further analyzed through a simple instance.

Use a C-Lambda expression

The complete code is as follows:

# Include <stdio. h>

 

/** Here is the macro definition of the Lambda expression we have defined */

# Define cgs_lambda (return_type, function_body )\

({Return_type cgs_lambda_func function_body cgs_lambda_func ;})

 

/*************************** Test Main ********** ****************/

Int main (int argc, const char ** argv)

{

/** Add two numbers */

Printf ("Sum = % d \ n", cgs_lambda (int, (int x, int y) {return x + y ;}) (3, 4 ));

 

Return 0;

}

 

You can try it by compiling and running. The running result is: Sum = 7.

Okay. Let's explain that return_type is int type, and function_body is the function body with two input parameters (int x, int y) {return x + y ;}, (3, 4) is the input parameter passed by cgs_lambda_func. In this way, you should know the role of "cgs_lambda_func;" in the cgs_lambda expression. Do not forget the Semicolon ";" at the end. Otherwise, the compilation will fail.

Let's translate what the cgs_lambda expression in the above example looks like after compilation, as follows:

({Int cgs_lambda_func (int x, int y) {return x + y;} cgs_lambda_func;}) (3, 4)

Conclusion

Thank you very much for your patience. I will update my blog and make a good job of my blog. Recently I have been busy writing small frameworks for network servers. Thank you for your attention, I wish you good health and good luck.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.