在C#中展平Ruby方法

时间:2008-10-13 09:17:38

标签: c# ruby arrays

如何在C#中执行Ruby方法"Flatten" Ruby Method。此方法将锯齿状数组展平为一维数组。

例如:

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

3 个答案:

答案 0 :(得分:12)

递归解决方案:

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

编辑1:

Jon在评论中解释为什么它不能成为通用方法,看一看!

编辑2:

Matt建议将其作为扩展方法。在这里,您只需将第一行替换为:

public static IEnumerable Flatten(this IEnumerable array)

你可以像这样使用它:

foreach(var item in myArray.Flatten()) { ... }

答案 1 :(得分:2)

我会在评论中回复,但我需要超过300个字符。

@ Alexander的解决方案非常棒,但它遇到了字符串数组的问题。由于string实现了IEnumerable,我认为它最终将返回每个字符串中的每个字符。您可以使用通用参数来告诉它在这些情况下您希望返回的内容,例如:

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

答案 2 :(得分:1)

难道你不能只使用IEnumerable#SelectMany?