将算法从C#转换为VB.NET失败 [英] Convert algorithm from C# to VB.NET fail
本文介绍了将算法从C#转换为VB.NET失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将以下算法从C#转换为VB.NET,而我拥有的VB.NET不能产生与C#算法相同的结果,有人可以告诉我我的转换哪里出错了吗?
I'm trying to convert the following algorithm from C# to VB.NET and the VB.NET I have is not producing the same results as my C# algorithm, can someone tell me where I've gone wrong in my conversion?
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中得到的:
This is what I've got in 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中。
Something is wrong, but I'm not sure what, I'm guessing the problem is somewhere in the lambda.
任何人都可以指出
推荐答案
使用一个代码转换器...
Use a code converter...
<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
这篇关于将算法从C#转换为VB.NET失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文