Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide overloads to avoid unnecessary boxing #71894

Merged
merged 1 commit into from
Feb 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,26 @@ public static ImmutableArray<TResult> SelectManyAsArray<TItem, TResult>(this Imm
return builder.ToImmutableAndFree();
}

/// <summary>
/// Maps and flattens a subset of immutable array to another immutable array.
/// </summary>
/// <typeparam name="TItem">Type of the source array items</typeparam>
/// <typeparam name="TResult">Type of the transformed array items</typeparam>
/// <param name="array">The array to transform</param>
/// <param name="selector">A transform function to apply to each element.</param>
/// <returns>If the array's length is 0, this will return an empty immutable array.</returns>
public static ImmutableArray<TResult> SelectManyAsArray<TItem, TResult>(this ImmutableArray<TItem> array, Func<TItem, ImmutableArray<TResult>> selector)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 There were 16 callers of the previous overload prior to this change. After this change, there are 3 callers of the previous overload, and 13 callers of the new overload.

{
if (array.Length == 0)
return ImmutableArray<TResult>.Empty;

var builder = ArrayBuilder<TResult>.GetInstance();
foreach (var item in array)
builder.AddRange(selector(item));

return builder.ToImmutableAndFree();
}

/// <summary>
/// Maps and flattens a subset of immutable array to another immutable array.
/// </summary>
Expand All @@ -288,17 +308,37 @@ public static ImmutableArray<TResult> SelectManyAsArray<TItem, TResult>(this Imm
public static ImmutableArray<TResult> SelectManyAsArray<TItem, TResult>(this ImmutableArray<TItem> array, Func<TItem, bool> predicate, Func<TItem, IEnumerable<TResult>> selector)
{
if (array.Length == 0)
{
return ImmutableArray<TResult>.Empty;

var builder = ArrayBuilder<TResult>.GetInstance();
foreach (var item in array)
{
if (predicate(item))
builder.AddRange(selector(item));
}

return builder.ToImmutableAndFree();
}

/// <summary>
/// Maps and flattens a subset of immutable array to another immutable array.
/// </summary>
/// <typeparam name="TItem">Type of the source array items</typeparam>
/// <typeparam name="TResult">Type of the transformed array items</typeparam>
/// <param name="array">The array to transform</param>
/// <param name="predicate">The condition to use for filtering the array content.</param>
/// <param name="selector">A transform function to apply to each element that is not filtered out by <paramref name="predicate"/>.</param>
/// <returns>If the items's length is 0, this will return an empty immutable array.</returns>
public static ImmutableArray<TResult> SelectManyAsArray<TItem, TResult>(this ImmutableArray<TItem> array, Func<TItem, bool> predicate, Func<TItem, ImmutableArray<TResult>> selector)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 There were two callers of the previous overload prior to this change. After this change, there is one caller of the previous overload, and one caller of the new overload.

{
if (array.Length == 0)
return ImmutableArray<TResult>.Empty;

var builder = ArrayBuilder<TResult>.GetInstance();
foreach (var item in array)
{
if (predicate(item))
{
builder.AddRange(selector(item));
}
}

return builder.ToImmutableAndFree();
Expand Down