我想從C#轉換下面的算法到VB.NET和VB.NET我沒有產生相同的結果作爲我的C#算法,有人可以告訴我在哪裏我在轉換中出錯了?從C#轉換算法到VB.NET失敗
public static IEnumerable<T[]> Combinations<T>(this IEnumerable<T> elements, int k)
{
List<T[]> result = new List<T[]>();
// single combination
if (k == 0)
{
result.Add(new T[0]);
}
else
{
int current = 1;
foreach (T element in elements)
{
//combine each element with k-1 combinations of subsequent elements
result.AddRange(elements
.Skip(current++)
.Combinations(k - 1)
.Select(combination => (new T[] { element }).Concat(combination).ToArray())
);
}
}
return result;
}
這是我在VB.NET中已經有了:
<Extension()>
Public Function Combinations(Of T)(ByRef elements As IEnumerable(Of T), ByVal k As Integer) As IEnumerable(Of T())
Dim result As New List(Of T())()
'single combination'
If k = 0 Then
result.Add(New T(-1) {})
Else
Dim current As Integer = 0
For Each element As T In elements
'combine each element with k - 1 combinations of subsequent elements'
Dim local As T = element
result.AddRange(elements.Skip(current = current + 1).Combinations(k - 1).Select(Function(combs) (New T() {local}).Concat(combs).ToArray()))
Next
End If
Return result
End Function
東西是錯的,但我不知道,我猜問題是在lambda地方。
任何人都可以指出我的轉換錯了嗎?
你有原始方法的單元測試嗎?如果沒有,請寫一些,然後在轉換的版本上運行它們。 – ashes999 2011-05-30 08:41:37