Created
February 4, 2025 20:13
-
-
Save cartercanedy/d17dd294c79517997af337f12c5c43b0 to your computer and use it in GitHub Desktop.
Combination of Select((T) => U) & Where((T) => bool) for better ergonomics
This file contains hidden or 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
using System.Collections.Generic; | |
public static class SelectWhereExtension { | |
delegate bool SelectWherePredicate<T, U>(T input, out U output); | |
public static IEnumerable<U> SelectWhere<T, U>(this IEnumerable<T> iter, SelectWherePredicate<T, U> predicate) { | |
var enumerator = iter.GetEnumerator(); | |
while (enumerator.MoveNext()) { | |
if (predicate(enumerator.Current, out U product)) { | |
yield return product; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment