Class TestPatterns
'<<1.Self-Defining Functions-自定义函数>>
Public Shared Sub SelfDefining()
Console.WriteLine(":: Pattern: Self-definining function") Dim foo As Action = Sub()
Console.WriteLine("Hi there!") foo = Sub()
Console.WriteLine("Hi again!")
End Sub
End Sub Console.WriteLine("First call (initilization).")
foo()
Console.WriteLine("Second call - use different one now!")
foo()
Console.WriteLine("Third call - still the same.")
foo()
End Sub '<<2.Callback Pattern-回调模式>>
Public Shared Sub Callback()
Console.WriteLine(":: Pattern: Callback pattern")
Console.WriteLine("Calling the function with lambda expression.") CallMe(Function() "The boss.") Console.WriteLine("Back at the starting point.")
End Sub Private Shared Sub CallMe(ByVal caller As Func(Of String))
Console.WriteLine("Received function as parameter - Who called?!")
Console.WriteLine(caller())
End Sub '<<3.Returning Functions-函数作为返回值>>
Public Shared Sub Returning()
Console.WriteLine(":: Pattern: Returning function")
Console.WriteLine("Calling to obtain the method ...")
Dim method As Func(Of Double, Double) = GetProperMethod("sin")
Console.WriteLine("Doing something with the method ...")
Console.WriteLine("f(pi / 4) = {0}", method(Math.PI / ))
End Sub Private Shared Function GetProperMethod(ByVal what As String) As Func(Of Double, Double)
Select Case what
Case "sin"
Return AddressOf Math.Sin
Case "cos"
Return AddressOf Math.Cos
Case "exp"
Return AddressOf Math.Exp
Case Else
Return Function(x) x
End Select
End Function '<<4.Immediately-Invoked Function Expression-立即调用的函数表达式>>
Public Shared Sub IIFE()
Console.WriteLine(":: Pattern: IIFE") DirectCast(Sub(x)
Console.WriteLine(2.0 * x * x - 0.5 * x) End Sub, Action(Of Double))(1.0) DirectCast(Sub(x, y)
Console.WriteLine(2.0 * x * y - 1.5 * x) End Sub, Action(Of Double, Double))(2.0, 3.0)
End Sub '<<5.Immediate Object Initialization-对象即时初始化>>
Public Shared Sub ImmediateObject()
Console.WriteLine(":: Pattern: Immediate object initialization") Dim terminator = New With { _
Key .Typ = "T1000", _
Key .Health = , _
Key .Hit = DirectCast(Function(x)
Return 100.0 * Math.Exp(-x) End Function, Func(Of Double, Double)) _
} Console.WriteLine("Terminator with type {0} has been created.", terminator.Typ)
Console.WriteLine("Let's hit the terminator with 0.5. Rest health would be {0}!", terminator.Hit(0.5))
End Sub '<<6.Init-Time Branching-初始化时间分支>>
Public Shared Sub InitTimeBranching()
Console.WriteLine(":: Pattern: Init-time branching")
Dim loopBody As Action(Of Integer) = Nothing
Console.WriteLine("Select a proper loop body method ...")
Dim r As New Random()
Dim sum As Integer = If r.NextDouble() < 0.5 Then
Console.WriteLine("Selected random choice ...") loopBody = Sub(index)
sum += r.[Next](, )
End Sub
Else
Console.WriteLine("Selected little gauss ...") loopBody = Sub(index)
sum += index
End Sub
End If Console.WriteLine("Execute the loop ...") For i As Integer = To
loopBody(i)
Next Console.WriteLine("Loop has finished with result sum = {0}.", sum)
End Sub End Class

引用于:http://www.cnblogs.com/gaochundong/p/lambda_design_patterns.html#immediately_invoked_function_expression_pattern

Lambda模式的更多相关文章

  1. 大数据小视角4:小议Lambda 与 Kappa 架构,不可变数据的计算探索

    这个系列文章之前因为私事荒废了很久,继续更新--之前与老大谈论架构时,老大和我聊了聊分布式数据处理之中的Lambda结构,之前在<Designing Data-Intensive Applica ...

  2. [搬运]在C#使用.NET设计模式的新观点

    原文地址:http://www.dotnetcurry.com/dotnet/1092/dotnet-design-patterns 软件开发有许多设计模式.其中一些模式非常受欢迎.说几乎所有的模式都 ...

  3. .NET面试题系列(一)基本概念

    什么是CLR CLR常用简写词语,CLR是公共语言运行库(Common Language Runtime)和Java虚拟机一样也是一个运行时环境,它负责资源管理(内存分配和垃圾收集等),并保证应用和底 ...

  4. 通过Flink实现个推海量消息数据的实时统计

    背景 消息报表主要用于统计消息任务的下发情况.比如,单条推送消息下发APP用户总量有多少,成功推送到手机的数量有多少,又有多少APP用户点击了弹窗通知并打开APP等.通过消息报表,我们可以很直观地看到 ...

  5. Webflux请求处理流程

    spring mvc处理流程 在了解SpringMvc的请求流程源码之后,理解WebFlux就容易的多,毕竟WebFlux处理流程是模仿Servlet另起炉灶的. 下面是spring mvc的请求处理 ...

  6. func-spring-boot-starter 快速上手

    func-spring-boot-starter test 项目地址 func-spring-boot-starter项目地址: https://gitee.com/yiur/func-spring- ...

  7. 匿名函数托管 func-spring-boot-starter

    匿名函数托管 func-spring-boot-starter 项目地址 func-spring-boot-starter开源项目地址: https://gitee.com/yiur/func-spr ...

  8. func-spring-boot-starter 匿名函数托管

    func-spring-boot-starter 匿名函数托管 GitHub项目路径: https://github.com/yiurhub/func-spring-boot-starter Gite ...

  9. Lambda应用模式

    Lambda应用模式 前言 在使用 Lambda 表达式时,我们常会碰到一些典型的应用场景,而从常用场景中抽取出来的应用方式可以描述为应用模式.这些模式可能不全是新的模式,有的参考自 JavaScri ...

随机推荐

  1. Java中各种集合(字符串类)的线程安全性!!!

    Java中各种集合(字符串类)的线程安全性!!! 一.概念: 线程安全:就是当多线程访问时,采用了加锁的机制:即当一个线程访问该类的某个数据时,会对这个数据进行保护,其他线程不能对其访问,直到该线程读 ...

  2. scala语言简介及其环境安装

    scala语言简介及其环境安装 简介: 1.运行在JVM 上,兼容java语言 Scala的代码,都需要经过编译为字节码,然后交由Java虚拟机来运行.所以Scala和Java是可以无缝互操作的.Sc ...

  3. OpenResty最佳实践

    https://moonbingbing.gitbooks.io/openresty-best-practices/content/

  4. PySpider问题记录http599

    问题简述 背景说明: python版本 3.5.2 操作流程: 1.执行pyspider安装: pip install pyspider 2.运行pyspider pyspider all 3.打开浏 ...

  5. css小贴士备忘录

    前言:在CSS的学习实践过程中,我经常遗忘一些貌似常用的代码,为了能够强化记忆特在此作归纳整理并将陆续增删,以备即时查阅.但愿今后能遇到问题及时解决,牢牢记住这些奇怪的字符们. 一.关于段落文本强制对 ...

  6. QVariant类及QVariant与自定义数据类型转换的方法

    这个类型相当于是Java里面的Object,它把绝大多数Qt提供的数据类型都封装起来,起到一个数据类型“擦除”的作用.比如我们的 table单元格可以是string,也可以是int,也可以是一个颜色值 ...

  7. 关于Unity中的NGUI字体

    NGUI字体类型 1: UIFont字体,UIFont类实现的2: TTF动态字体的使用3: BBCode的特殊字体的使用4: NGUI字体制作5: BMFont字体制作和艺术字体的制作6: UILa ...

  8. Self20171218_TestNG+Maven+IDEA环境搭建

    前言: 主要进行TestNG测试环境的搭建 所需环境: 1.IDEA UItimate 2.JDK 3.Maven 一.创建工程 File –>new –>Project–>next ...

  9. Opengl绘制我们的小屋(一)球体,立方体绘制

    这个系列我想用来运用opengl红皮书的前八章节的内容,来打造一个室内小屋. 这一章主要是定义几个基本的结构.并给出球体与立方体的画法,先让我们来定义一些基本的结构.一个是包含点,法向量,纹理贴图向量 ...

  10. Pandas 读取文本格式数据

    title 其实书中说的我认为不够全,因为公司里面现在主要用stata和spss,暂时还没有用到sas,excel也很少用 那么读取文件的方式,因为有人已经总结了,我就偷过来算了 对应不同的文件类型有 ...