-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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 a new signature to Sum
to be able to do: Sum((x, i) => ...)
#59336
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
I think a good label would be |
Sum()
Sum
to be able to do: Sum((x, i) => ...)
Tagging subscribers to this area: @eiriktsarpalis Issue DetailsBackground and motivationHello, While writing a Luhn check algorithm, I realized that I often write a source.Select((x, i) => (x, i)).Sum(t => t.x + t.i); Instead I would love to be able to just do: source.Sum((x, i) => x + i); Of course, we will have to a new function per numeric type (e.g. decimal, double, float, etc.). I am willing to take on implementing it. API Proposalpublic static int Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, int, int> selector)
{
if (source == null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source);
}
if (selector == null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector);
}
int sum = 0;
int index = 0;
checked
{
foreach (TSource item in source)
{
sum += selector(item, index);
index += 1;
}
}
return sum;
} I choose a naive implementation to be as close as possible to the existing code. API Usagevar c = new []{1, 2, 3,};
var s = c.Sum((x, i) => i % 2 == 0 ? x * 2 : x);
// Getting the value out
Console.WriteLine(s); // 10 Here is the code used for the demo: public static class LinqExtensions
{
public static int Sumi(this IEnumerable<int> source, Func<int, int, int> selector) => source.Select(selector).Sum();
} RisksI don't know any risks linked to this new signature. This contribution maintain API signature and behavioral compatibility. This contribution does not include any breaking changes.
|
Adding more LINQ method overloads comes at the expense of shared framework size, so before accepting a proposal we would need to see evidence that 1) the proposed addition solves a problem that many users are likely to encounter and 2) it is difficult to work around the problem in the absence of the proposed API. Summation that takes the element index into account is a relatively niche application, and can be easily worked around using the approaches you proposed, or assuming you would like to avoid tuples: source.Select((i,x) => i + x).Sum(); Alternatively, it should be fairly easy to roll an extension method of your own. Furthermore, as you've already pointed out, for completeness this would require us adding overloads for every supported arithmetic type: Therefore I don't believe this proposal would meet the bar for inclusion in System.Linq. |
@eiriktsarpalis - what about adding an index to |
I agree with everything else you said, but for this specific claim I'd hope we could utilize a single generic overload constrained to one of the new arithmetic interfaces. |
I think that question could be extended to any LINQ function that takes a delegate acting on individual elements: e.g. |
Agreed. Noting that this would be dependent on We'd also need to determine if simply having |
@tannergooding do we have an issue tracking API additions that take advantage of generic arithmetic? |
Nope, I'd be fine with a label, project board, or similar provided there aren't any issues with doing that. CC. @jeffhandley |
I recommend creating a public, org-level project board for this. That would allow us to collect proposals across all of our repositories, and look at them holistically across runtime, aspnetcore, and others. |
In the meantime, I'm going to close this issue since the specific request does not meet the bar for inclusion in System.Linq. Thank you for your contribution @aloisdg. |
@eiriktsarpalis you are welcome. Even if the request was rejected, I am glad to have submitted my first issue here :) |
Background and motivation
Hello,
While writing a Luhn check algorithm, I realized that I often write a
Select
before doing aSum
to get the index. LikeSelect
,Sum
can already take aFunc<TSource, int> selector
as parameter but there is now way to get the index. We have to write:Instead I would love to be able to just do:
Of course, we will have to a new function per numeric type (e.g. decimal, double, float, etc.).
I am willing to take on implementing it.
API Proposal
I choose a naive implementation to be as close as possible to the existing code. The code would be written in Sum.cs
API Usage
You can try this online.
Here is the code used for the demo:
Risks
I don't know any risks linked to this new signature. This contribution maintain API signature and behavioral compatibility. This contribution does not include any breaking changes.
The text was updated successfully, but these errors were encountered: