4 ms·
> Even the JS standard library struggles with this. ... I don't think this represents struggling. There needs to be some way to sort in place. Sometimes you
by recursive 2y ago
> Even the JS standard library struggles with this. ...
I don't think this represents struggling. There needs to be some way to sort in place. Sometimes you need to sort a big array and don't want to allocate.
And there should be some way to clone the array without mutating. That's slice. So you how do you sort a clone? .slice().sort()
I think by far the biggest problem with ES .sort() is that number arrays don't sort numerically by default.
- steveklabnik 2y ago> There needs to be some way to sort in place. Sometimes you need to sort a big array and don't want to allocate Your parent isn't saying there shouldn't be mutation, just that the mutation should be obvious. In Rust, the type signature for the in-place sort is pub fn sort(&mut self) where T: Ord, That `&mut self` lets you know that it's going to mutate.
- josephg 2y agoExactly. And for function parameters, rust also (usually) makes it obvious when you’re passing by value, by reference or by mutable reference: foo(x); // Moves or copies foo(&x); // immutable reference foo(&mut x); // mutable reference I don’t need to look up the signature of foo to understand what happens to my variable. It’s obvious at a glance.
- recursive 2y agoFair point. I guess this is the cost of dynamic languages.