理解Lambda表達式和閉包

瞭解由函數指針到Lambda表達式的演化過程程序員

Lambda表達式的這種簡潔的語法並非什麼古老的祕法,由於它並不難以理解(難以理解的代碼只有一個目的,那就是嚇唬程序員)閉包

 1 #include "stdafx.h"
 2 using namespace System;
 3 
 4 typedef void(*FunctionPointer)(System::String ^str);
 5 
 6 void HelloWorld(System::String ^str)
 7 {
 8     Console::WriteLine(str);
 9     Console::ReadLine();
10 }
11 
12 int main(array<System::String ^> ^args)
13 {
14     FunctionPointer fp = HelloWorld;
15     fp("Hello World");
16     return 0;
17 }
函數指針
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 
 5 namespace CharpFunctionPointer {
 6     class Program {
 7 
 8         delegate void FunctionPointer(string str);
 9 
10         static void Main(string[] args) {
11             FunctionPointer fp = HelloWorld;
12             fp("Hello World!");
13         }
14 
15         static void HelloWorld(string str) {
16             Console.WriteLine(str);
17             Console.ReadLine();
18         }
19     }
20 
21 }
委託
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 
 5 namespace CharpFunctionPointer {
 6     class Program {
 7 
 8         delegate void FunctionPointer(string str);
 9 
10         static void Main(string[] args) {
11             FunctionPointer fp = delegate (string s) {
12                 Console.WriteLine(s);
13                 Console.ReadLine();
14             };
15             fp("Hello World!");
16         }
17     }
18 
19 }
匿名委託
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 
 5 namespace CharpFunctionPointer {
 6     class Program {
 7 
 8         delegate void FunctionPointer(string str);
 9 
10         static void Main(string[] args) {
11             FunctionPointer fp =
12                 s => Console.WriteLine(s);
13 
14             fp("Hello World!");
15             Console.ReadLine();
16         }
17     }
18 }
Lambda表達式

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 
 5 namespace CharpFunctionPointer {
 6     class Program {
 7 
 8         static void Main(string[] args) {
 9             Action<string> fp = s => Console.WriteLine(s);
10 
11             fp("Hello World!");
12             Console.ReadLine();
13         }
14     }
15 }
將Lambda表達式賦值給一個預約義的泛型委託

Lambda表達式和閉包ide

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 using System.Linq;
 5 
 6 namespace LambdaClosure {
 7     class Program {
 8 
 9         static void Main(string[] args) {
10             UsesClosure();
11         }
12 
13         static void UsesClosure() {
14             string toFind = "ed";
15             var words = new string[] {
16                 "ended","friend","closed","potato"
17             };
18 
19             var matches = words.Select(s => s.Contains(toFind));
20             foreach(var str in matches) {
21                 Console.WriteLine(str);
22             }
23             Console.ReadLine();
24         }
25     }
26 }
使用本地變量toFind就會通知編譯器生成一個閉包(或包裝器類),這樣,toFind就不會成爲"未定義"

柯里化(Currying)函數

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 using System.Linq;
 5 
 6 namespace LambdaCurry {
 7     class Program {
 8 
 9         static void Main(string[] args) {
10             Func<int,int,int> longLambda = (x,y) => x + y;
11             Console.WriteLine(longLambda(3,4));
12 
13             //currying
14             Func<int,Func<int,int>> curry = x => y => x + y;
15             Console.WriteLine(curry(3)(4));
16 
17             Console.ReadLine();
18         }
19     }
20 }
LambdaCurry
相關文章
相關標籤/搜索