News for hackers. Stuff that matters.
Re:Clever code is probably the worst code you could write by neonsunset on Tuesday May 21, 2024 @06:54PM (#40435092)
C++ makes this (and many other things!) needlessly painful.

In C# it is just

    return numbers.Sum();
Re:Clever code is probably the worst code you could write by vlovich123 on Tuesday May 21, 2024 @08:30PM (#40436005)
Same for Rust.
Re:Clever code is probably the worst code you could write by the__alchemist on Tuesday May 21, 2024 @10:38PM (#40436816)
I tend to use for loops in Rust for this reason; simple, and understandable by anyone who's programmed in an imperative language.

The one-liner approach tends to include an explicit type declaration (Or turbofish), `iter()`, and `collect()`, at minimum.

Re:Clever code is probably the worst code you could write by cptroot on Wednesday May 22, 2024 @10:39PM (#40449807)
FYI I think you've misinterpreted the parent. To my eyes it looks like they're agreeing that Rust makes summing a list easy using combinations. For example, this is how I would do it in Rust:

  x.iter().sum()
Re:Clever code is probably the worst code you could write by vlovich123 on Thursday May 23, 2024 @04:02AM (#40451877)
I think they’ve understood and are claiming that a simple loop is cleaner than what you wrote. I’m not sure how since that 1 line communicates what’s happening at a high level with 1 word “sum” whereas a loop requires me to read the loop to confirm it’s a plain summation.

This sounds like someone complaining that the concepts they learned when they were first starting out are the only concepts that programming languages should introduce which is silly - technology evolves as we find better and new ways of accomplishing old tasks.

Re:Clever code is probably the worst code you could write by tialaramex on Tuesday May 21, 2024 @07:19PM (#40435391)
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()

Re:Clever code is probably the worst code you could write by nyssos on Tuesday May 21, 2024 @07:42PM (#40435639)
> 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/Sy...

Re:Clever code is probably the worst code you could write by neonsunset on Tuesday May 21, 2024 @07:22PM (#40435436)
C# arrays don't. However, C# arrays are IEnumerable<T>, which does.
Re:Clever code is probably the worst code you could write by lupire on Tuesday May 21, 2024 @07:32PM (#40435535)
IEnunerable<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.enu...

Re:Clever code is probably the worst code you could write by masfuerte on Tuesday May 21, 2024 @07:35PM (#40435564)
So you can't use IEnumerable<T>.Sum if T is a custom type implementing operator+ ?
Re:Clever code is probably the worst code you could write by neonsunset on Tuesday May 21, 2024 @07:49PM (#40435690)
You 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/m...

[1]: https://github.com/dotnet/runtime/issues/64031

Re:Clever code is probably the worst code you could write by lupire on Tuesday May 21, 2024 @07:29PM (#40435509)
This 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...

Don't make bold dismissive comments about things you are ignorant of. It makes you look Blubby.

Re:Clever code is probably the worst code you could write by tialaramex on Tuesday May 21, 2024 @07:59PM (#40435773)
The 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.

Re:Clever code is probably the worst code you could write by neonsunset on Tuesday May 21, 2024 @08:21PM (#40435949)
There 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/Sy...

[1]: 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)

Re:Clever code is probably the worst code you could write by tialaramex on Wednesday May 22, 2024 @07:40AM (#40439772)
> (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)

The "in most cases" is doing all the lifting here. Rust's Iterator is in the prelude. Even if you #![no_std] you get the core prelude which has core::iter::Iterator. You would need to explicitly write code to tell Rust "No, I don't want the prelude" to get rid of it, I'm sure people do that, otherwise it wouldn't be possible, but few enough that I've never seen it.

But in C# there is no such promise. In most (but not all) real world C# projects somebody already brought in LINQ. If you're using a technology that gives you a "ready to go" standard C# project template it undoubtedly folds in LINQ too. But it's not actually provided by the language and that's a meaningful gap.

Notably in several of the playground type tools, since LINQ is not there by default this won't work - like I said it won't compile and it doesn't suggest "Oh you need LINQ" because the C# compiler doesn't provide such suggestions.

Re:Clever code is probably the worst code you could write by neonsunset on Wednesday May 22, 2024 @09:01AM (#40440412)
It's difficult to talk to someone committed to misreading the replies.

LINQ aka IEnumerable/Iterator methods are usually imported by default, something like with prelude. As others said, they are part of standard library.

I'm not sure what you are arguing against either but whatever your criticism is - it is misplaced as C# and Rust roughly belong to the same Venn diagram of features against commonly held beliefs.

You can test it yourself:

    sudo apt install dotnet-sdk-8.0
    mkdir TestConsole && cd TestConsole
    dotnet new console
And then just paste/echo the following to Program.cs:

    var numbers = Enumerable.Range(0, 10).ToArray();
    var sum = numbers.Sum();
    Console.WriteLine(sum);
This will compile. If that's not enough, then it's likely Rust the language is not for you either and you may want to use something like Go for the time being.
Re:Clever code is probably the worst code you could write by latentsea on Tuesday May 21, 2024 @09:24PM (#40436364)
Usage of LINQ is usually taken for granted.
Re:Clever code is probably the worst code you could write by wvenable on Tuesday May 21, 2024 @09:28PM (#40436404)
LINQ is just part of .NET standard library along with the very collections we are talking about. I'm not sure why one would care about the distinction.