Skip to content

Commit

Permalink
Split SelectWhere into two methods for better nullable representation.
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahelsaig committed Jul 23, 2024
1 parent 66cffff commit 49a3019
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions Lombiq.HelpfulLibraries.Common/Extensions/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,18 +167,31 @@ public static IList<T> AsList<T>(this IEnumerable<T> collection) =>

/// <summary>
/// Transforms the specified <paramref name="collection"/> with the <paramref name="select"/> function and returns
/// the items that are not null. Or if the <paramref name="where"/> function is given then those that return <see
/// langword="true"/> with it.
/// the items that return <see langword="true"/> when passed to the <paramref name="where"/> function.
/// </summary>
public static IEnumerable<TOut> SelectWhere<TIn, TOut>(
this IEnumerable<TIn> collection,
Func<TIn, TOut> select,
Func<TOut, bool>? where = null)
Func<TOut, bool> where)
{
foreach (var item in collection)
{
var converted = select(item);
if (where?.Invoke(converted) ?? converted is not null) yield return converted;
if (where.Invoke(converted)) yield return converted;
}
}

/// <summary>
/// Transforms the specified <paramref name="collection"/> with the <paramref name="select"/> function and returns
/// the items that are not null.
/// </summary>
public static IEnumerable<TOut> SelectWhere<TIn, TOut>(this IEnumerable<TIn> collection, Func<TIn, TOut?> select)
where TOut : notnull
{
foreach (var item in collection)
{
var converted = select(item);
if (converted is not null) yield return converted;
}
}

Expand Down

0 comments on commit 49a3019

Please sign in to comment.