gpt4 book ai didi

c# - 将算法从 C# 转换为 VB.NET 失败

转载 作者:行者123 更新时间:2023-11-30 12:35:06 25 4
gpt4 key购买 nike

我正在尝试将以下算法从 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

关于c# - 将算法从 C# 转换为 VB.NET 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6174043/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com