3 ms·
https://github.com/golang/go/wiki/SliceTricks https://github.com/golang/go/wiki/SliceTricks I was reading through this, which was referenced from the article. T
by fmakunbound 6y ago
https://github.com/golang/go/wiki/SliceTricks 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.
- cutler 6y agoMe too. Made me realise I picked the wrong language just for a performance boost. The Go patterns mentioned earlier are even worse.
- Measter 6y agoMeanwhile, 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&edition=2018&gist=7fed5e30b0d4fab58f9231df19b4a46f https://play.rust-lang.org/?version=stable&mode=debug&editio...
- Dr_Emann 6y agoExpand 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`.
- Measter 6y agoThat's not exactly the same. Splice returns an iterator, it doesn't modify the collection in place.
- Dr_Emann 6y agoYes 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&edition=2018&gist=207a1c4de84ed43608ab774c15c8e5db https://play.rust-lang.org/?version=stable&mode=debug&editio...
- Measter 6y agoHuh... I guess I should read the docs more carefully next time; I clearly misunderstood them.
- Dr_Emann 6y agoAnd for move to front, why not `a[..i+1].rotate_right(1);`, rather than moving to the back and then shifting everything?
- Measter 6y agoUh... 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.