【发布时间】:2011-08-20 21:09:29
【问题描述】:
我有这样的课程:
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) 这是否违反了static classes should not have state这句话?
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)
等等,但显然它根本不能很好地扩展。
【问题讨论】:
-
我觉得它更像是“静态类应该避免状态”。
-
@Pace:你真的不需要用“vb C# .net”开始每个问题
-
@John Saunders 我认为这会使问题更加“可见”..
-
@Pacerier:请不要为了能见度而那样标记它们。如果是 VB.NET 问题标签,则为
[vb.net]。我们最不需要的就是让它变得像[c]和[c++]标签一样。 -
@Jeff Mercado 我不太明白你想说什么,但没关系。
标签: .net vb.net function memoization