Created
March 28, 2023 06:59
-
-
Save mirmostafa/97f6150867a244d4893c789710a99d67 to your computer and use it in GitHub Desktop.
Mix of Iterator Patteren and Pattern Matching
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
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