3 ms·
Where did "numbers" come from, and why are you so sure you can Sum() it? The original C code offered has some data structure (perhaps an array?) called x. Do C#
by tialaramex 2y ago
Where did "numbers" come from, and why are you so sure you can Sum() it? The original C code offered has some data structure (perhaps an array?) called x. Do C# arrays have a Sum method? I don't think so.
In Rust you would probably just write: x.iter().sum()
- neonsunset 2y agoC# arrays don't. However, C# arrays are IEnumerable<T>, which does.
- lupire 2y agoIEnunerable<T> of course does not have Sum. Only collections of numeric types (including arrays) have Sum. https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.sum?view=net-8.0 https://learn.microsoft.com/en-us/dotnet/api/system.linq.enu...
- deleted 2y ago[deleted]
- masfuerte 2y agoSo you can't use IEnumerable<T>.Sum if T is a custom type implementing operator+ ?
- deleted 2y ago[deleted]
- neonsunset 2y agoYou could, but .Sum is a little more specific than that - it is non-overflowing sum. Historically, constraining generic arguments on addition was problematic - the full feature set of numeric types was "lifted" to be fully representable through generics only recently[0]. With that said, there is an open proposal[1] to introduce additional generic math overloads to IEnumerable<T> methods, but it hasn't seen much activity as the existing overloads cover most commonly used numeric types already. [0]: https://learn.microsoft.com/en-us/dotnet/standard/generics/math https://learn.microsoft.com/en-us/dotnet/standard/generics/m... [1]: https://github.com/dotnet/runtime/issues/64031 https://github.com/dotnet/runtime/issues/64031
- lupire 2y agoThis comment makes no sense. You dismissed the C# code, and then wrote the same thing in Rust, but with an extra non-conceptually-meaningful boilerplate step. May as well ask, "where did x come from, and why are you so sure you can iter().sum() it?" C# has generic types, so yes, C# arrays of numbers have a Sum method. https://stackoverflow.com/questions/2419343/how-to-sum-up-an-array-of-integers-in-c-sharp https://stackoverflow.com/questions/2419343/how-to-sum-up-an... Don't make bold dismissive comments about things you are ignorant of. It makes you look Blubby.
- tialaramex 2y agoThe C# code ends up relying on LINQ, it's interesting how many C# programmers don't even think about that, either anything they work on already uses LINQ or they just reflexively bring it in everywhere they write C# You'll see that a few of those SO comments actually say they're relying on LINQ to make that work. The array type doesn't have such a method itself. So, in reality although many C# programmers will think of this as "correct" it just won't even compile... except if there's already LINQ.
- neonsunset 2y agoThere is nothing wrong with System.Linq just like there's nothing wrong with Rust's std::iter::Iterator. If anything, this makes writing Rust use the existing muscle memory if you have C# experience and vice versa. The performance profile of LINQ, while much maligned, has been steadily improving over the years and, in the example of Sum itself, you actually do want to use it because it will sum faster than open-coded loop[0]. I do have grievances regarding LINQ still - my (non-negotiable) standpoint is that Roslyn (C# compiler) must lower non-escaping LINQ operations to open-coded loops, inline lambdas at IL level and similar, making it zero-cost - .NET (IL compiler/runtime) provides all the tools necessary to match what Rust's zero-cost-ish iterator expressions offer and there just needs to be more political will in the Roslyn teams to do so. Because of this, I'm holding my breath waiting for DistIL[1] to be production-ready which does just that. [0]: https://github.com/dotnet/runtime/blob/main/src/libraries/System.Linq/src/System/Linq/Sum.cs#L80 https://github.com/dotnet/runtime/blob/main/src/libraries/Sy... [1]: https://github.com/dubiousconst282/DistIL https://github.com/dubiousconst282/DistIL (I don't understand what you mean by "it won't compile", because it will, in most cases System.Linq namespace is already referenced anyway, either through global usings or at the top of the file)
- nyssos 2y ago> Do C# arrays have a Sum method? I don't think so. It's not literally a method (it's not, for instance, in the vtable of some array class), but as far as their API is concerned, yeah, they do: https://github.com/dotnet/runtime/blob/main/src/libraries/System.Linq/src/System/Linq/Sum.cs https://github.com/dotnet/runtime/blob/main/src/libraries/Sy...