申し訳ありませんが、コードチャンクは長いですが、ラムダと匿名機能で使用できるさまざまなオプションを示したいと思います。
まず、我々は...
'Solves a basic linear equation y(x) = ax + b, given a, b, and x.
Function Linear(a As Double, b As Double, x As Double) As Double
Return a * x + b
End Function
'Return the inverse of a number (i.e. y(x) = -x)
Function Inverse(x As Double) As Double
Return -x
End Function
と機能を取る関数と遊ぶためにいくつかの基本的な機能を作成します。
'To help differentiate the type of the parameter from the return type,
'I'm being generic with the return type. This function takes any function
'that takes a double and returns some generic type, T.
Public Function EvalEquation(Of T)(x As Double, equation As Func(Of Double, T)) As T
Return equation(x)
End Function
最後に、これを使用します。
'The closest thing to a functor is probably the AddressOf keyword.
For x = 0 To 10
Dim answer = EvalEquation(x, AddressOf Inverse)
'Do something
Next
しかし、AddressOfにはいくつかの制限があります。 EvalEquationForXはただ1つのパラメータをとる関数を必要とします。したがって、余分なパラメータを渡すことができないため、単にAddressOfを使用することはできません。しかし、私はそれを行うことができる機能を動的に作成することができます。
For x = 0 To 10
Dim answer = EvalEquation(x, Function(x)
Dim a = 1
Dim b = 0
Return Linear(a, b, x)
End Function)
'Do something
Next
私はあなたがFunc(Of T1, T2, T3, T4,... TResult)
を定義できることに注意しなければならないので、あなたは、2つのパラメータと使用の代わりを取ることができる機能を作成することができます。
Public Function EvalEquationWithTwoParameters(Of T)(
a As Double, b As Double, x As Double,
equation As Func(Of Double, Double, Double, T)) As T
Return equation(a, b, x)
End Function
そして、このようにそれを使用します。
For x = 0 To 10
Dim answer = EvalEquationWithTwoParameters(1, 0, x, AddressOf Linear)
'Do something
Next
希望に役立ちます!