For some project I needed to process a given number of combinations of items, for example all combinations of three items from a longer list. Instead of hardcoding loops (and one method for each additional combination level) or attempting my own recursion I searched web and found this short method using Enumerable extensions and recursion that happened to solve that quickly. FYI this allows a single iteration for any given combination count, thus a single method solution. The code I found was written in C# and there was some problems converting it since I could not first see where VB was complaining (it was mainly suggesting problems inferring parameter types). After some attempts there it was and I thought sharing this neat extension method, so here it is:
With generics and the given type list you can see how this quickly gets complicated without inferring
Note: This works only for VB 2010, it ~didn't work~ in VB 2008.
Original code was found posted in thread: Algorithm to return all combinations of k elements from n - Stack Overflow
The actual return is IEnumerable(Of IEnumerable(Of T)), which means first level of return is all combination 'groups' and next level is the item combination. In the below practical example first level is each line, and second level is the three items.
Here is a sample output, to easier see what it does, imagine item list {A,B,C,D} and you want all combinations of three elements; items.Combinations(3):
I can't exactly recall when, but I'm sure I have encountered uses for this several times before this round. Anyway, it is also an interesting study with all those Enumerable calls, think about what they do also in relation being recursive
VB.NET:
Imports System.Runtime.CompilerServices
Module Extensions
<Extension()> Public Function Combinations(Of T)(ByVal elements As IEnumerable(Of T), ByVal k As Integer) As IEnumerable(Of IEnumerable(Of T))
Return If(k = 0, {New T(-1) {}}, elements.SelectMany(Function(e, i) elements.Skip(i + 1).Combinations(k - 1).Select(Function(c) (New T() {e}).Concat(c))))
End Function
End Module
Note: This works only for VB 2010, it ~didn't work~ in VB 2008.
Original code was found posted in thread: Algorithm to return all combinations of k elements from n - Stack Overflow
The actual return is IEnumerable(Of IEnumerable(Of T)), which means first level of return is all combination 'groups' and next level is the item combination. In the below practical example first level is each line, and second level is the three items.
Here is a sample output, to easier see what it does, imagine item list {A,B,C,D} and you want all combinations of three elements; items.Combinations(3):
According to this article Permutations, Combinations, and Variations using C# Generics - CodeProject this is called "combinations without repetitions" (ie AB equals BA, so no 'variations').A, B, C
A, B, D
A, C, D
B, C, D
I can't exactly recall when, but I'm sure I have encountered uses for this several times before this round. Anyway, it is also an interesting study with all those Enumerable calls, think about what they do also in relation being recursive