我正在尝试将以下算法从 C# 转换为 VB.NET,但我所拥有的 VB.NET 生成的结果与我的 C# 算法不同,有人可以告诉我在转换过程中哪里出了问题吗?
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 中的某个地方。
有人能指出我的转换做错了什么吗?
使用代码转换器...
<System.Runtime.CompilerServices.Extension> _
Public Shared Function Combinations(Of T)(elements As IEnumerable(Of T), 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 = 1
For Each element As T In elements
'combine each element with k-1 combinations of subsequent elements
result.AddRange(elements.Skip(System.Math.Max(System.Threading.Interlocked.Increment(current),current - 1)).Combinations(k - 1).[Select](Function(combination) (New T() {element}).Concat(combination).ToArray()))
Next
End If
Return result
End Function
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)