please explain to me this code (C#)

Updated on May 7, 2017 in [A] C# .Net
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
1 on May 6, 2017

I was looking for c# solution for combinations and i found this code:
public static IEnumerable<IEnumerable<T>> Combinations<T>(this IEnumerable<T> elements, int k)
{
return k == 0 ? new[] { new T[0] } :
elements
.SelectMany((e, i) =>
elements
.Skip(i + 1).Combinations(k 1).Select(c => (new[] {e}).Concat(c)));

}
can you explain it to me line by line? pls

  • Liked by
Reply
0 on May 7, 2017

static keyword before return type and this keyword before first parameter makes this method an extension method. This method can be called using any IEnumerable collection object of any type.(Read more about extension methods)
Possible usage can be like this:

IEnumerable<int> myCollection = Enumerable.Range(1, 10);
IEnumerable<int> result = myCollection.Combinations(3);

“T” in the code defines that the extension method is generic, you can replace any data type with t when using it. I have used int for example.

This method is a recursive method which is calling itself in certain condition. Recursive methods always have two parts. Terminating part (returns somthing) and recusrive part (where it calls itself). Recursive Methods

Now if method is called with 0 as parameter. It will call this line and will return a new array of the specified data type. This is returning/terminating part of the method.

return k == 0 ? new[] { new T[0] } :

Recursive part: the code after “:” is recursive part as it is calling Combination method again. This syntax is called conditional operator which is widely used instead of if-else statement. Here is a simple example:

bool b = false;
int a = (b == true) ? 1 : 0; // if b is true, a will be 1 else a will be 0.

So else part is a bit tricky here. Let me break it down for you:

// note that IEnumerator is a collection of some values of type T.

elements.SelectMany(
// SelectMany returns a collection by projecting the source collection(elements) based on the selector anonymous method which is starting from (e,i)

    (e, i) => // this is a selector which takes an element (e) in collection and an int as index (i)
// the code below will run for every element in collection. just like any for loop.

        elements.Skip(i + 1).  // this will skip i+1 elements and rest of the code will be applied on result
        Combinations(k 1).  // this will take result from previous and call the method recursively but using k-1 (if method was called using 2 originally now it will be called using 1a and same goes on untill it is called using 0 and gets termianted)

        Select(c => (new[] {e}). // now the final result returned from recursive call. it will construct an array containing the element at current index (e) and then the result of “Select” will be passed to next line

        Concat(c)));  // Concat will add all the arrays and return the result in SelectMany which then will be returned from the method.

Here are some helpful links:
Skip Extension Method
Difference between Select and SelectMany

I hope this helps.

Aparently this is my very first interaction at Brackeys as I can’t donate to this cause in any other way. I am contributing on this forums.

  • Liked by
Reply
Cancel