zoukankan      html  css  js  c++  java
  • 理解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表达式和闭包

     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
  • 相关阅读:
    简单的分页存储过程,Json格式日期转换为一般日期
    事件的那些事
    关于1Byte 1K 1M 1G(换算)
    VS自带WCF测试客户端简单介绍
    “System.Transactions.Diagnostics.DiagnosticTrace”的类型初始值设定项引发异常[WCF]
    周末大放送网站图片上传,水印,预览,截图
    FIREDAC驱动ORACLE的配置
    匿名方法实现多线程同步到主线程执行
    DELPHI跨平台的临界替代者
    三层数据库设计注意事项
  • 原文地址:https://www.cnblogs.com/revoid/p/6664290.html
Copyright © 2011-2022 走看看