Created
November 6, 2013 02:20
-
-
Save sergeyt/7329871 to your computer and use it in GitHub Desktop.
flatten enumerable extension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal static class Extensions | |
{ | |
public static IEnumerable<T> Flatten<T>(this IEnumerable<T> source, Func<T, IEnumerable<T>> childSelector) | |
{ | |
foreach (var item in source) | |
{ | |
yield return item; | |
foreach (var d in childSelector(item).Flatten(childSelector)) | |
{ | |
yield return d; | |
} | |
} | |
} | |
public static IEnumerable<T> Flatten<T>(this T source, Func<T, IEnumerable<T>> childSelector) | |
{ | |
yield return source; | |
foreach (var d in childSelector(source).Flatten(childSelector)) | |
{ | |
yield return d; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment