I am trying to convert the following algorithm from C # to VB.NET and VB.NET, which I have, does not produce the same results as my C # algorithm, can someone tell me where I made a mistake 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; }
Here is what I 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
Something is wrong, but I'm not sure that, I assume the problem is somewhere in lambda.
Can anyone point out what I did wrong with my conversion?
source share