-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
[API Proposal]: Add Enumerable.Concat & Flatten overloads #54220
Comments
Tagging subscribers to this area: @eiriktsarpalis Issue DetailsBackground and MotivationWhen working with different APIs, often times it increases code readability to aggregate results and then linearize them. For example, I could write the code:
or I could write:
The second is much more clear and concise. Proposed API
Usage Examples
Alternative DesignsJust use RisksNone
|
I'm assuming |
Note that another way to write this code is to use the existing overload of Command.NameInfo.OnlineAccounts =
Input.WebAddresses.Select(x => CreateWebAddress(x))
.Concat(Input.EmailAddresses.Select(x => CreateEmailAddress(x)))
.ToList(); Or: Command.NameInfo.OnlineAccounts = Enumerable.Concat(
Input.WebAddresses.Select(x => CreateWebAddress(x)),
Input.EmailAddresses.Select(x => CreateEmailAddress(x))
).ToList(); But you do have to choose between the fluent assymetric form and the non-fluent symmetric form. So I think the proposed overload of |
Agreed, @TonyValenti if you could update the OP following the API proposal template we can consider this proposal for .NET 7. |
I often make a |
@danielchalmers please consider upvoting the original post, it provides a good signal on whether a particular proposal is popular! |
Hi @TonyValenti, before I can mark this ready for API review would it be possible to update the original post so that it follows our API review template? |
@eiriktsarpalis - Maybe I'm missing something, but I think it does. This is the format, right? |
A few issues with the original text I would like to call out:
The API review document contains links to a few API proposal issues, if you're looking for real-world examples. |
@eiriktsarpalis Thanks! I just updated it. I left the implementation just because I felt that added more clarity. I also added a params overload for Concat that will make joining multiple enumerables easier. |
@TonyValenti I've updated your proposal slightly to better match the template. The reason we want this is to allow the API review committee quickly read and understand what is being proposed. |
Great! Thanks so much! |
namespace System.Linq
{
public static partial class Enumerable
{
public static IEnumerable<TSource> Concat<TSource>(
this IEnumerable<TSource> first,
IEnumerable<TSource> second,
params IEnumerable<TSource>[] rest);
public static IEnumerable<TSource> Flatten<TSource>(this IEnumerable<IEnumerable<TSource>> sources);
}
public static partial class Queryable
{
public static IQueryable<TSource> Concat<TSource>(
this IQueryable<TSource> source1,
IEnumerable<TSource> source2,
params IEnumerable<TSource>[] rest);
public static IQueryable<TSource> Flatten<TSource>(this IQueryable<IEnumerable<TSource>> sources);
}
} |
@bartonjs Why does the public static IEnumerable<TSource> Concat<TSource>(
this IEnumerable<TSource> first,
params IEnumerable<TSource>[] rest); |
@svick we wanted to prevent concatenation with zero parameters, i.e. |
I just realized that I completely forgot about IQueryable overloads: namespace System.Linq
{
public static partial class Queryable
{
public static IQueryable<TSource> Concat<TSource>(
this IQueryable<TSource> source1,
IEnumerable<TSource> source2,
params IEnumerable<TSource>[] rest);
public static IQueryable<TSource> Flatten<TSource>(this IQueryable<IEnumerable<TSource>> sources);
}
} @bartonjs should we be reviewing those seperately? |
Did we discuss params Span at all? Assuming that shows up in C# 11, would we still use params arrays here, would we add both, or would we say as a general rule we prefer spans moving forward? |
My recollection is we have a blanket policy of "oh, right, make queryable look the same as enumerable" |
We did. Partly it'll end up depending on what it looks like for those methods to be called by VB/F#. If it Just Works, then @terrajobst suggested we'd go back and look at new-in-7 API that we could remove the redundancies from. If it doesn't just work, then we'll probably add them in parallel anywhere other than low-level types. |
This issue has been marked with the When ready to submit an amended proposal, please ensure that the original post in this issue has been updated, following the API proposal template and examples as provided in the guidelines. |
Changed to |
No progress for .NET 8? Does |
It would be nice to have a |
With collection literals and the spread operator, I assume this example would be completely nullified since you can just do: Command.NameInfo.OnlineAccounts = [
..Input.WebAddresses.Select(x => CreateWebAddress(x)),
..Input.EmailAddresses.Select(x => CreateEmailAddress(x))
]; Or am I missing something? |
Background and Motivation
When working with different APIs, often times it increases code readability to aggregate results and then linearize them. For example, I could write the code:
or I could write:
The second is much more clear and concise.
Proposed API
Usage Examples
Alternative Designs
Just use
SelectMany(x => x)
Risks
None other than System.Linq size concerns.
The text was updated successfully, but these errors were encountered: