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 / 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 Sub


End Class

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值