Skip to content

Instantly share code, notes, and snippets.

@cartercanedy
Created February 4, 2025 20:13
Show Gist options
  • Save cartercanedy/d17dd294c79517997af337f12c5c43b0 to your computer and use it in GitHub Desktop.
Save cartercanedy/d17dd294c79517997af337f12c5c43b0 to your computer and use it in GitHub Desktop.
Combination of Select((T) => U) & Where((T) => bool) for better ergonomics
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