博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Lambda模式
阅读量:6036 次
发布时间:2019-06-20

本文共 3973 字,大约阅读时间需要 13 分钟。

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 / 4))    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 = 100, _         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 = 0        If r.NextDouble() < 0.5 Then            Console.WriteLine("Selected random choice ...")            loopBody = Sub(index)                           sum += r.[Next](0, 10000)                       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 = 0 To 9999            loopBody(i)        Next        Console.WriteLine("Loop has finished with result sum = {0}.", sum)    End SubEnd Class

引用于:

转载地址:http://eblhx.baihongyu.com/

你可能感兴趣的文章
laravel 集合接口
查看>>
C/C++二进制读写png文件
查看>>
thymleaf 常用th 标签
查看>>
RTB 广告系统
查看>>
Linux signal 那些事儿(2)【转】
查看>>
InfluxDB安装及配置
查看>>
Dynamics CRM Microsoft SQL Server 指定的数据库具有更高的版本号
查看>>
PAT Perfect Sequence (25)
查看>>
java.exe进程来源排查录
查看>>
点滴记录——Ubuntu 14.04中Solr与Tomcat整合安装
查看>>
C++实现KMP模式匹配算法
查看>>
ubuntu linux下建立stm32开发环境: GCC安装以及工程Makefile建立
查看>>
记录锁
查看>>
JSONObject与JSONArray的使用
查看>>
[SQL Server] 数据库日志文件自动增长导致连接超时的分析
查看>>
【常见Web应用安全问题】---6、Script source code disclosure
查看>>
<html:form>标签
查看>>
除了《一无所有》,我一无所有
查看>>
每日英语:China Seeks to Calm Anxiety Over Rice
查看>>
C++中struct和class的区别 [转]
查看>>