Hacker News new | ask | show | jobs
by dazhbog 846 days ago
Glad the pinnacle of programming is still there: left-pad
2 comments

I think this time it won't spread as much because most people just ask their LLM for a leftpad function.
What are the chances that the LLM suggests the NPM package?
The entire source code:

    export function leftPad(str, len, ch) {
      return new Array(len - str.length).fill(ch || ch === 0 ? ch : ' ').join('') + str
    }
It's not even needed anymore: "HN".padStart(10); That's it.
Does pad start respect RTL marks? /s
Seems like the logical way to do it. Though, honestly, unless you specifically need to do lots of different kinds of padding, something like this makes way more sense:

const bespokePad = (x) => ('000000' + x).slice(-6)

that actually seems really inefficient, it could avoid the array allocation and fill call by just moving the length check...
You could also just use `String.prototype.padStart()` [1], and you get a nice optimized C++ implementation :D

[1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

While it's not quite C++, here is Chromium's implementation: https://source.chromium.org/chromium/chromium/src/+/main:v8/...
It's Torque, which is essentially C++

https://v8.dev/_img/docs/torque/build-process.svg

While funny, worth remembering that .padStart didn't exist when left-pad was first published :)
Pass, not a Rust implementation /s
Is it even AI scale if not performed on the GPU?
No, you're right. The White House says no more C/C++ it's your civic duty.
I wanna see you write it from memory. Now do it without bugs fixed in 2013 in that package.