4 ms·
Leftpad-rs
- jrnkntl 11y agoIs there a special reason the tests are all asserting inequality? To me this only indicates if it doesn't do stuff it's not supposed to do, instead of working as intended: https://github.com/hfiguiere/leftpad-rs/blob/master/src/lib.rs#L29:L39 https://github.com/hfiguiere/leftpad-rs/blob/master/src/lib.... [edit] thanks for the explanation, I'm not familiar with rust and just assumed the bang would mean !=
- thecatspaw 11y agoit doesnt test for inequality. assert_eq! is a macro (indicated by the exclamation mark)
- deleted 11y ago[deleted]
- palmr 11y agoThe assert_eq!() calls are calls to the assert equals macro: https://doc.rust-lang.org/std/macro.assert_eq!.html https://doc.rust-lang.org/std/macro.assert_eq!.html In rust macro functions look like functions but end in a bang(!), it's not using ! to mean negate. http://rustbyexample.com/macros.html http://rustbyexample.com/macros.html
- deskamess 11y agoThe ! indicates that assert_eq is a macro call. Usage example: https://doc.rust-lang.org/std/macro.assert_eq!.html https://doc.rust-lang.org/std/macro.assert_eq!.html
- deleted 11y ago[deleted]
- zelcon5 11y agoLol this is all pretty funny. I also did this but in C++ to make fun of that JS crap. https://gist.github.com/zelcon5/7dc42bf91ea958132a0d https://gist.github.com/zelcon5/7dc42bf91ea958132a0d - "padding" can go either left or right. - generic function - minimal lines of code Wow so great, make me a dependency for Windows11, Apache, Firefox, and toasters.
- xgbi 11y agoSo, what is the point of this? Highlight that it is possible to do to Cargo what leftpad has made to NPM?
- cornstalks 11y agoSatire, most likely.
- tomtomtom777 11y agoOr aiming for > 2000000 downloads ;)
- hub_ 11y agodefinitely not.
- deleted 11y ago[deleted]
- IceyEC 11y agoBut it isn't; you can yank a crate on crates.io and that will stop new installs but it will still be available for installs that have it in their Cargo.lock file
- hub_ 11y agoI wrote this as a practical joke, following a friend of mine who did that for Go. (and no I didn't post that to HN either)
- lhecker 11y agoYour code is a bit inefficient. I took the liberty of fixing that for you: https://gist.github.com/lhecker/8e89d998c156f0bfdd2d https://gist.github.com/lhecker/8e89d998c156f0bfdd2d (You can use that code freely and without any attribution if you want to.) The important part here is to use `String::with_capacity()` to prevent unnecessary reallocations. Furthermore I used `usize::saturating_sub()` because it's neat and cleaned up the code with `rustfmt` (which you should use).
- hub_ 11y agoa PR is more practical if you want me to merge. Thanks !
- hub_ 11y agobtw, creating the string with capacity was in my mind at one point. Some contributor submitted a PR already https://github.com/hfiguiere/leftpad-rs/pull/2 https://github.com/hfiguiere/leftpad-rs/pull/2 Sadly it has gathered some rust (pun intended !)
- kennywinker 11y agoI see i'm not the only one who did this. Leftpad for Swift https://github.com/coryalder/SwiftLeftpad https://github.com/coryalder/SwiftLeftpad
- lhecker 11y agoYour version is quite inefficient too... If you call `insert(_:atIndex:)` N times it will obviously move the entire content of the string N times by only a single character. The optimal solution would be instead if you create a `CollectionType` which repeats the padding character N times. That way you could use `insertContentsOf(_:at:)` to insert N characters at once into the string and move the contents of the string only once. If that doesn't work for you might as well simply allocate a new `String`, reserve `pad` chars of space, insert the `pad` chars and then insert the previous string. Even that is probably still faster than your previous approach. In the end it's still probably the easiest approach in practice to simply use the `NSString` method `stringByPaddingToLength(_:withString:startingAtIndex:)`... :D
- kennywinker 11y agoThanks for the code review. To me this is the ultimate irony of this whole thing. Many have read it as "you should have written your own leftpad function, it's 11 lines!!" But no bit of code is too short to be buggy / inefficient. Centralizing it allows us to optimize/fix once. While my implementation is basically a direct port of node's leftpad, pull requests are welcome. :)
- DanSmooth 11y agoinb4 "leftpad-as-a-service"...
- venomsnake 11y agoLeftpad should be the new fizzbuzz in interviews.
- hub_ 11y agothere is a big irony in that comment... (very personal to me - that no one can get here)
- foota 11y agoI'd hate to see how the borrow checker destroys objects lent to remote systems... Along the vein of https://xkcd.com/416/ https://xkcd.com/416/