tags:

views:

665

answers:

3

How can I do the Ruby method "Flatten" Ruby Method in C#. This method flattens a jagged array into a single-dimensional array.

For example:

s = [ 1, 2, 3 ]           #=> [1, 2, 3]
t = [ 4, 5, 6, [7, 8] ]   #=> [4, 5, 6, [7, 8]]
a = [ s, t, 9, 10 ]       #=> [[1, 2, 3], [4, 5, 6, [7, 8]], 9, 10]
a.flatten                 #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10
+11  A: 

Recursive solution:

IEnumerable Flatten(IEnumerable array)
{
    foreach(var item in array)
    {
        if(item is IEnumerable)
        {
            foreach(var subitem in Flatten((IEnumerable)item))
            {
                yield return subitem;
            }
        }
        else
        {
            yield return item;
        }
    }
}

EDIT 1:

Jon explains in the comments why it cannot be a generic method, take a look!

EDIT 2:

Matt suggested making it an extension method. Here you go, just replace the first line with:

public static IEnumerable Flatten(this IEnumerable array)

and you can use it like this:

foreach(var item in myArray.Flatten()) { ... }
Alexander Kojevnikov
My initial thought was "Why isn't it generic?" - but of course it can't be because only very rarely is an iterable version of T also a T. (e.g. IEnumerable<object> is still an object, but IEnumerable<string> isn't a string.)It may be worth clarifying this in the answer.
Jon Skeet
Also I'm not sure that there's any way to declare a strongly-typed ragged array in C# is there? It has to be object[], which means IEnumerable is a reasonable parameter type for this method.
Matt Hamilton
Matt: Not sure what you mean by "strongly-typed ragged array" but int[][] and int[,] (for jagged and rectangular arrays of ints respectively) are fine.
Jon Skeet
@Matt: Indeed, that's exactly what I thought.
Alexander Kojevnikov
@Jon: I always try to do the easiest approach that works first. Apparently it's also the most generic: int[][] and int[,] are both IEnumerables. If the caller of flatten() knows the types in advance she can always cast to them.
Alexander Kojevnikov
@Alexander: Sorry, I wasn't clear. I believe you've got exactly the right answer, but it may be worth a bit of explanation as to why making it a generic method (i.e. Flatten<T>) would be counterproductive. (You can't cast the result to IEnumerable<T> though - you'd have to use Enumerable.Cast<T>.)
Jon Skeet
@Jon I mean a ragged array with single values as well as nested arrays. What type is {1, 2, {3, {4, 5}}, 6}? Some of its elements are arrays and others are int.
Matt Hamilton
@Alexander This would be even nicer as an extension method!
Matt Hamilton
@Jon, @Matt: I updated the answer with your suggestions.
Alexander Kojevnikov
Not to nitpick too much but shouldn't method names all be capitalized "Flatten" vs "flatten"?
lfoust
+1  A: 

I would have responded in a comment, but I need more than 300 characters.

@Alexander's solution is awesome, but it runs into a problem with arrays of strings. Since string implements IEnumerable, I think it will end up returning each character in every string. You can use a generic parameter to tell it what kind of thing you are hoping to have returned in these cases, e.g.:

public static IEnumerable Flatten<T>(IEnumerable e)
{
    if (e == null) yield break;
    foreach (var item in e)
    {
        if (item is T)
           yield return (T)item;
        else if (item is IEnumerable)
        {
            foreach (var subitem in Flatten<T>((IEnumerable)item))
                yield return subitem;
        }
        else
           yield return item;
    }
}
Matt
+1  A: 

Couldn't you just use IEnumerable#SelectMany?

Raving Genius