关于实施记忆的2个问题
|
我有一个这样的课:
Public NotInheritable Class F
Private Sub New()
End Sub
Public Shared Function Mize(Of TResult)(ByVal f As System.Func(Of TResult)) As System.Func(Of TResult)
Dim is_new = True
Dim result As TResult
Return Function()
If is_new Then
result = f()
End If
Return result
End Function
End Function
Public Shared Function Mize(Of T, TResult)(ByVal f As System.Func(Of T, TResult)) As System.Func(Of T, TResult)
Dim is_new_s = New System.Collections.Generic.List(Of Boolean)
Dim inputs = New System.Collections.Generic.List(Of T)
Dim d = New System.Collections.Generic.Dictionary(Of T, TResult)
Return Function(arg1 As T)
If d.ContainsKey(arg1) Then
Return d.Item(arg1)
Else
Dim result = f(arg1)
d.Add(arg1, result)
Return result
End If
End Function
End Function End Class
我想知道
1)这是否违反了静态类不应该具有状态的说法?
2)如何修改函数以使其可以接受任何函数(而不是上面的情况仅适用于F(TResult)
和F(T, TResult)
。我的意思是我可以创建另一个函数:
Function Mize(Of T, T2, TResult)(ByVal f As System.Func(Of T, T2, TResult))
As System.Func(Of T, T2, TResult)
依此类推,但显然它根本无法很好地扩展。
没有找到相关结果
已邀请:
1 个回复
庞协腿宛炯
一样,对任意数量的参数进行变形,直到最大(如10或20?)。 使用ѭ5作为键(and6作为函数),而不是泛型类型。这会降低类型安全性,并可能导致速度显着降低,并且仅当调用ѭ7的成本被函数速度所抵消时才应使用它。 使用支持模板的另一种语言(例如C ++,D或Scheme)(这不是一个非常简单的选择,但无论如何我都提到过)。 例如记忆在某些语言中很容易,例如D:
可以像这样容易使用:
不,您的示例没有违反状态原理,因为您的静态类不保持状态! (实际上,您每次都在捕获局部变量,而不重用以前的任何东西。)不过,我的确实如此。