Recursive List Flattening

后端 未结 13 1363
既然无缘
既然无缘 2020-11-27 04:25

I could probably write this myself, but the specific way I\'m trying to accomplish it is throwing me off. I\'m trying to write a generic extension method similar to the oth

13条回答
  •  旧时难觅i
    2020-11-27 05:32

    Okay here's another version which is combined from about 3 answers above.

    Recursive. Uses yield. Generic. Optional filter predicate. Optional selection function. About as concise as I could make it.

        public static IEnumerable Flatten(
            this IEnumerable nodes, 
            Func filterBy = null,
            Func> selectChildren = null
            )
        {
            if (nodes == null) yield break;
            if (filterBy != null) nodes = nodes.Where(filterBy);
    
            foreach (var node in nodes)
            {
                yield return node;
    
                var children = (selectChildren == null)
                    ? node as IEnumerable
                    : selectChildren(node);
    
                if (children == null) continue;
    
                foreach (var child in children.Flatten(filterBy, selectChildren))
                {
                    yield return child;
                }
            }
        }
    

    Usage:

    // With filter predicate, with selection function
    var flatList = nodes.Flatten(n => n.IsDeleted == false, n => n.Children);
    

提交回复
热议问题