Flatten Ruby method in C#

老子叫甜甜 提交于 2019-12-21 16:57:43

问题


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

回答1:


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()) { ... }



回答2:


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;
    }
}



回答3:


Couldn't you just use IEnumerable#SelectMany?



来源:https://stackoverflow.com/questions/197081/flatten-ruby-method-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!