Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

https://github.com/golang/go/wiki/SliceTricks I was reading through this, which was referenced from the article. This just seems insane. None of any of that seems intuitive.


Meanwhile, in the much more complicated Rust, all but four of those are one-liners[0]. The exceptions being:

* Expand. Rust doesn't provide an easy way to insert multiple values into the middle of a vector, so I had to do 2 lines: append, then rotate right a subslice starting at the desired insertion point by the length of the insertion.

* Shuffle. No RNG in Rust's stdlib, so I used Rand. I did re-implement the shuffle, but in reality I'd probably just use the shuffle function provided by Rand.

* In place dedup. Needed two lines: one to sort, then I could call dedup.

* Move to front. This is not a function Rust provides, so it's completely implemented. Needed 10 lines. This one needs to search for the item and move that if it exists. In the event it exists, I rotate it left onto the end of the vector, then rotate the entire vector right. Otherwise it inserts at the beginning.

[0] https://play.rust-lang.org/?version=stable&mode=debug&editio...


Expand does actually exist in the standard library, but it's a bit of a strange incantation...

    vec.splice(i..i, std::iter::repeat(0).take(length));
This replaces the values in range `i..i` (an empty range starting at i) with `length` copies of `0`.


That's not exactly the same. Splice returns an iterator, it doesn't modify the collection in place.


Yes it does exactly that. It returns an iterator over the removed items, but when that iterator is dropped, it modifies the collection. See https://play.rust-lang.org/?version=stable&mode=debug&editio...


Huh... I guess I should read the docs more carefully next time; I clearly misunderstood them.


And for move to front, why not `a[..i+1].rotate_right(1);`, rather than moving to the back and then shifting everything?


Uh... because I'd been up for 20 hours and by brain fixated on a weird way to do it?

Don't program when tired, kids.


Me too. Made me realise I picked the wrong language just for a performance boost. The Go patterns mentioned earlier are even worse.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: