Skip to content

Instantly share code, notes, and snippets.

@mirmostafa
Created March 28, 2023 06:59
Show Gist options
  • Save mirmostafa/97f6150867a244d4893c789710a99d67 to your computer and use it in GitHub Desktop.
Save mirmostafa/97f6150867a244d4893c789710a99d67 to your computer and use it in GitHub Desktop.
Mix of Iterator Patteren and Pattern Matching
public static IEnumerable<T> AddRangeImmuted<T>(this IEnumerable<T>? source, IEnumerable<T>? items)
{
return (source, items) switch
{
(null, null) => Enumerable.Empty<T>(),
(_, null) => source,
(null, _) => items,
(_, _) => addRangeImmutedIterator(source, items)
};
static IEnumerable<T> addRangeImmutedIterator(IEnumerable<T> source, IEnumerable<T> items)
{
foreach (var item in source)
{
yield return item;
}
foreach (var item in items)
{
yield return item;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment