Programming Skills: Use Lambda as a local Function

Source: Internet
Author: User
Prerequisites: the scope of variables and functions should be minimized.
According to this principle, if function a is called only in function B, function A should be defined and used in function B, that is, function a is defined as a local function within function B.
Note: functions here generally refer to methods of classes in OOP and Lambda in FP.
The following uses the C # code as an example.
using System;namespace ConsoleApplication1{    class Program    {        static int f(int a)        {            g(a, "a");            g(a, "b");            return a;        }        static void g(int a, string b)        {            Console.WriteLine("a={0},b=\"{1}\"", a, b);        }        static void Main(string[] args)        {            int n = f(1) + f(2);            Console.WriteLine("n={0}", n);        }    }}/*a=1,b="a"a=1,b="b"a=2,b="a"a=2,b="b"n=3*/
The above code is not surprising, but it is not difficult to find that function f () is called only in main, and function g () is called only in F.
If a local function exists in the language, function f () should be defined in main according to the principle of minimizing the function scope, and function g () should be defined in function f.
Although C # does not have a real local function, we can regard Lambda as a substitute for a local function.
The following is an example of using lambda:
using System;namespace ConsoleApplication1{    class Program    {        static void Main(string[] args)        {            Func<int, int> f = a =>            {                Action<string> g = b =>                    Console.WriteLine("a={0},b=\"{1}\"", a, b);                g("a");                g("b");                return a;            };            int n = f(1) + f(2);            Console.WriteLine("n={0}", n);        }    }}
When using Lambda as a local function in C #, pay attention to the following points:
  1. Lambda is equivalent to an unnamed function. It can be named by assigning values, but cannot be overloaded.
  2. Lambda functions have no default parameter.
  3. It is extremely difficult to use Lambda to directly define recursive functions. You can define them by declaring null first and then defining function entities.
  4. Since Lambda can automatically capture the variables of peripheral functions to form closures, using Lambda to define local functions can often reduce the number of parameters. This is another advantage of local functions beyond the "minimal function scope.
C ++ version:
#include <iostream>#include <string>using namespace std;int main(){auto f = [](int a)->int{auto g = [&](const string& b){cout << "a=" << a << ",b=" << b << endl;};g("a");g("b");return a;};int n = f(1) + f(2);cout << "n=" << n << endl;return 0;}

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.