Lambda模式
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
Lambda模式的更多相关文章
- 大数据小视角4:小议Lambda 与 Kappa 架构,不可变数据的计算探索
这个系列文章之前因为私事荒废了很久,继续更新--之前与老大谈论架构时,老大和我聊了聊分布式数据处理之中的Lambda结构,之前在<Designing Data-Intensive Applica ...
- [搬运]在C#使用.NET设计模式的新观点
原文地址:http://www.dotnetcurry.com/dotnet/1092/dotnet-design-patterns 软件开发有许多设计模式.其中一些模式非常受欢迎.说几乎所有的模式都 ...
- .NET面试题系列(一)基本概念
什么是CLR CLR常用简写词语,CLR是公共语言运行库(Common Language Runtime)和Java虚拟机一样也是一个运行时环境,它负责资源管理(内存分配和垃圾收集等),并保证应用和底 ...
- 通过Flink实现个推海量消息数据的实时统计
背景 消息报表主要用于统计消息任务的下发情况.比如,单条推送消息下发APP用户总量有多少,成功推送到手机的数量有多少,又有多少APP用户点击了弹窗通知并打开APP等.通过消息报表,我们可以很直观地看到 ...
- Webflux请求处理流程
spring mvc处理流程 在了解SpringMvc的请求流程源码之后,理解WebFlux就容易的多,毕竟WebFlux处理流程是模仿Servlet另起炉灶的. 下面是spring mvc的请求处理 ...
- func-spring-boot-starter 快速上手
func-spring-boot-starter test 项目地址 func-spring-boot-starter项目地址: https://gitee.com/yiur/func-spring- ...
- 匿名函数托管 func-spring-boot-starter
匿名函数托管 func-spring-boot-starter 项目地址 func-spring-boot-starter开源项目地址: https://gitee.com/yiur/func-spr ...
- func-spring-boot-starter 匿名函数托管
func-spring-boot-starter 匿名函数托管 GitHub项目路径: https://github.com/yiurhub/func-spring-boot-starter Gite ...
- Lambda应用模式
Lambda应用模式 前言 在使用 Lambda 表达式时,我们常会碰到一些典型的应用场景,而从常用场景中抽取出来的应用方式可以描述为应用模式.这些模式可能不全是新的模式,有的参考自 JavaScri ...
随机推荐
- Excel数据批量导入到SqlServer的方法
1,以Excel为数据源建立连接导入. 关键点在于Excel的数据要有表头,表头要和数据库表的列名一样.连接字符串中HDR=YES不能省略,也就是第一行是表头的意思.IMEX=1;是把数据都当作字符串 ...
- 《C++程序设计教程——给予Visual Studio 2008》读书笔记3章
CLR(Common Language Runtime,通用运行时),负责在执行时管理代码,提供内存管理和线程管理等核心服务,同时又确保代码的安全性和准确性.
- webpack2--webpack 4.X 快速创建demo
准备工作 1.新建文件夹:webpack-demo(下面我们简称该文件夹为根目录),在根目录下面建两个文件夹,分别为src和dist. 1).src文件夹:用来存放我们编写的javascript代码, ...
- sparkr脚本
test <- function(){ print(1) } test() 脚本内为单独函数时 调用脚本不执行 所以要在脚本内调取特定函数
- 树莓派3 U盘启动 配置 & 即 MSD启动 总结
树莓派3添加了一个新特性:允许USB启动.现在我们既可以从SD卡启动,也可以从USB启动.USB设备可以是U盘,带USB适配器的SSD硬盘,甚至是移动硬盘. 本文介绍怎么从U盘启动树莓派3. 1. ...
- 【转】VSCode Web开发插件
Web开发必备插件 1.Beautify - HTML.CSS.JS.JSON语法高亮 语法高亮,你懂的. Beautify 2.Auto rename tag - 自动重命名标签 在HTML, ...
- 为什么要有handler机制?handler机制的原理
为什么要有handler机制? 在Android的UI开发中,我们经常会使用Handler来控制主UI程序的界面变化.有关Handler的作用,我们总结为:与其他线程协同工作,接收其他线程的消息并通过 ...
- C#自定义事件 范例:监视变量变化
很多时候我们需要程序具有一种功能,就是当满足某一条件时触发某个动作,使用C#的事件机制就可以达到这个目的下面的例子是一个很好的演示,这段代码实现了对一个变量的监视,一旦变量发生改变,就触发动作 定义事 ...
- Python装饰器、metaclass、abc模块学习笔记
(博客原创作品,转载请注明出处!) 最近接触到了Python中的decorator,metaclass,abc Module,six.add_metaclass等内容,这里做一个简单的笔记. 主要资源 ...
- 让不支持MVC3的服务器/空间/虚拟主机 支持MVC3 (前提是必须支持 .net 4.0才可以)
在有的时候,你用的VPS,服务器或者是虚拟空间可能是没有安装MVC3的,下面我们就自己来手动来让虚拟空间支持(前提是空间必须是.net 4.0的,因为MVC3是在4.0下运行) 我们先在web.con ...