Lambda示例大集合

Module Module1
    '“lambda 表达式”是一种无名函数,用于计算并返回单个值。lambda 表达式可在委托类型有效的任何地方使用。

    Sub Main()

        'Dim add = Function(num As Integer) num + 1
        'Console.WriteLine(add(5))
        'Test()
        'TestDele()
        'TestNull()
        'ProcessList()
        'TestArray()
        Test01()
        Console.ReadLine()
    End Sub

#Region "判断类型"

    Sub Test()
        TestResult(4, Function(num) num Mod 2 = 0)
        TestResult(5, Function(num) num > 10)
    End Sub

    Sub TestResult(ByVal value As Integer, ByVal fun As Func(Of Integer, Boolean))
        If fun(value) Then
            Console.WriteLine("success")
        Else
            Console.WriteLine("Failure")
        End If
    End Sub
#End Region

#Region "在LINQ"
    '在语言集成查询 (LINQ) 中,lambda 表达式成为许多标准查询运算符的基础。
    '编译器创建 lambda 表达式以捕获基础查询方法(例如 Where、Select、Order By、Take While 以及其他方法)中定义的计算。
    Sub LinQTest()
        'Dim LondonCust = From cust In Dda.customers Where cust.city = "Londom" Select cust

        'Dim Londoncust =DBa.customers .where(function (cust) cust .city ="Londom" .Select (Function (cust) cust)
    End Sub
#End Region


    Delegate Function ExampleDel(ByVal arg1 As Integer, ByVal arg2 As String) As Integer
    Sub TestDele()
        Dim del As ExampleDel = Function(m, s) m
        Console.WriteLine(del(7, "Up"))

    End Sub


    '如果可以为 null 的参数已被赋值,则该表达式返回 True;如果该参数的值为 Nothing,则返回 False。
    Sub TestNull()

        Dim NotNothing = Function(num? As Integer) num IsNot Nothing

        Dim arg As Integer = 14
        Console.WriteLine("Does the argument have an assigned value?")
        Console.WriteLine(NotNothing(arg))

    End Sub


#Region "测试两数操作"

    Delegate Function MathOperator(ByVal x As Double, ByVal y As Double) As Double

    Function AddNumbers(ByVal x As Double, ByVal y As Double) As Double
        Return x + y
    End Function

    Function SubtractNumbers(ByVal x As Double, ByVal y As Double) As Double
        Return x - y
    End Function

    Sub DelegateTest(ByVal x As Double, ByVal op As MathOperator, ByVal y As Double)
        Dim ret As Double
        ret = op.Invoke(x, y) ' Call the method.
        MsgBox(ret)
    End Sub

    Public Sub TestAb()
        DelegateTest(5, AddressOf AddNumbers, 3)
        DelegateTest(9, AddressOf SubtractNumbers, 3)
    End Sub

#End Region

#Region "查找进程"
    Sub ProcessList()
        '创建一个进程数组
        Dim ProList As Process() = Diagnostics.Process.GetProcesses

        '返回有两个线程的进程。
        Dim FilteredList = ProList.Where(Function(p) p.Threads.Count = 2)

        '显示每个进程的名称
        For Each proc In FilteredList
            Console.WriteLine(proc.ProcessName)
        Next

    End Sub

    '用语言集成查询 (LINQ) 语法编写的代码
    Sub ProcessOtheList()
        Dim filteredQuery = From proc In Diagnostics.Process.GetProcesses Where
                           proc.Threads.Count = 2 Select proc

        For Each proc In filteredQuery
            Console.WriteLine(proc.ProcessName)
        Next
    End Sub
#End Region


#Region "数组"
    '该表达式返回数组中最后一个元素的索引
    Sub TestArray()
        Dim Numbers() As Integer = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
        Dim LastIndex = Function(intArray() As Integer) intArray.Length - 1

        For i = 0 To LastIndex(Numbers)
            Numbers(i) = Numbers(i) + 1
            Console.WriteLine(Numbers(i))
        Next
    End Sub

#End Region


#Region "last"

    Sub Test01()
        ' Variable takeAGuess is a Boolean function. It stores the target
        ' number that is set in makeTheGame.
        Dim takeAGuess As gameDelegate = makeTheGame()

        ' Set up the loop to play the game.
        Dim guess As Integer
        Dim gameOver = False
        While Not gameOver
            guess = CInt(InputBox("Enter a number between 1 and 10 (0 to quit)", "Guessing Game", "0"))
            ' A guess of 0 means you want to give up.
            If guess = 0 Then
                gameOver = True
            Else
                ' Tests your guess and announces whether you are correct. Method takeAGuess
                ' is called multiple times with different guesses. The target value is not 
                ' accessible from Main and is not passed in.
                gameOver = takeAGuess(guess)
                Console.WriteLine("Guess of " & guess & " is " & gameOver)
            End If
        End While

    End Sub

    Delegate Function gameDelegate(ByVal aGuess As Integer) As Boolean

    Public Function makeTheGame() As gameDelegate

        ' Generate the target number, between 1 and 10. Notice that 
        ' target is a local variable. After you return from makeTheGame,
        ' it is not directly accessible.
        Randomize()
        Dim target As Integer = CInt(Int(10 * Rnd() + 1))

        ' Print the answer if you want to be sure the game is not cheating
        ' by changing the target at each guess.
        Console.WriteLine("(Peeking at the answer) The target is " & target)

        ' The game is returned as a lambda expression. The lambda expression
        ' carries with it the environment in which it was created. This 
        ' environment includes the target number. Note that only the current
        ' guess is a parameter to the returned lambda expression, not the target. 

        ' Does the guess equal the target?
        Dim playTheGame = Function(guess As Integer) guess = target

        Return playTheGame

    End Function


#End Region
End Module

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值