lobsters score: 1 · tags: programming, rust
I wonder why NonEmpty is defined like that, with the head element stored separately from the rest of the vec. It means you have to reallocate + copy the entire vec in the constructor and you can’t take a slice of all the elements. It also inflates the size of NonEmpty.
It would be good enough to assert the non-emptiness of the underlying vec in the constructor and retain the vec as-is. It would also simplify the implementation considerably, especially
sortwhich does an extra binary search and two O(n) operations after the actual sort.
score 1 · canonical
The whole idea is “make invalid state impossible to represent”. The head element cannot be empty, therefore constructing the type is enough to prove the collection is non empty.
Doing it your way is possible but requires the programmer who implemented it to have done so in a bug free way.
It means you have to reallocate + copy the entire vec in the constructor
IIRC the compilation process can optimize away some of that. Depending on how it is used.
It would be interesting to see a comparison to see the real world differences. Also there might be implementations with more efficient implementations out there.
score 1 · canonical
Love that the question was “why” and my answer for why got negative downvotes. Sure there alternatives, maybe those alternatives are better. Also…someone asking “why” and getting met with a genuine attempt at an answer should never get downvoted IMHO. Also the downvote is marking this as spam which? I’m not sure what I’m spamming? A paid advertisement for “big type” ??!?!?
Ahh, this is literally a duplicate comment. That makes sense. Lobsters has been hitting me with anubis failures on my iOS phone when I’m typing by hand and I had to type this one out and submit it several times to get it to work. I didn’t realize some of the others actually went through.
score 1 · canonical
