doing things that make me feel gross

(it's

let i = 0;
for (; i<matches[0].length; i++) {
const idx = i + currentSignal.length;
if (matches[0][idx] !== matches[matches.length-1][idx]) break;
}

)

Follow

@monorail I think a while loop would be better but I'm not sure I would give enough of a shit in a code review because either way would look Weird

@wallhackio ??? Iterating i over a range of numbers is a very typical use for a for loop, why are you saying that a while loop would be better?!

@alas @wallhackio Oh. Something like this?

[removed because it's full of mistakes and I'm ashamed]

This does make sense from a control flow perspective, sorry for not seeing it immediately

@aescling @alas @wallhackio Oops. I'm stupid. And that's a much easier mistake to make in a while-loop than in a do-loop where for (i=0; /* something */, i++) is the most common idiom

@vaporeon_ @alas @wallhackio but anyway, if you wrote a loop ending in i++ in the body, my furst thought would be “this is a for loop”

@aescling @alas @wallhackio Yes, exactly, I think the thing I typed up with do { ... } while ( ... ); is just worse in this case. Curious how Clodsire would do it differently if he prefers a while loop.

@vaporeon_ @alas oh I wouldn't have thought to do it with a do while, this is neat

my evil code golfing brain wants the i++ snuck into the previous line but that kind of thing tends to get you in trouble

by the way you also need i<matches[0].length in the conditional

@wallhackio @alas Augh. My brain really is not working today. :blobcat_notlikethis: And see, this is why I would prefer a for-loop here. Iterate over all i in a particular range and break early if some condition happens. Common pattern and less likely for me to make stupid mistakes about ranges or increments due to being sleepy.

Really want to know how you would've written it with a while loop that's less hideous and ugly and wrong than what I've done.

@wallhackio @vaporeon_ @alas what it actually needs for my use case that i only realized later is to break on first === undefined || last === undefined || first !=== last

it's a "longest common prefix" algorithm

@monorail @wallhackio @alas So that it breaks when either of the arrays end, even if last is shorter than first? Is that the reason for checking the undefined? Can you also do that with

for (; i<matches[0].length&&i<matches[matches.length-1].length; i++)

Or am I making a stupid mistake again?

@vaporeon_ first I would try to avoid the break by having a more complicated conditional containing the logic put in the if statement

and when I have a gnarly conditional I find it easier to read in a while loop instead of a for loop since the for loop statement contains three sections

but that's all a matter of style, really

@wallhackio @vaporeon_ i bet you could write this loop in the form for (; [nasty conditional]; i++) { }, which would be the funniest option

@aescling @vaporeon_ whenever I end up writing something like that I tend to convert it to a while loop but that's just a style thing

@wallhackio @vaporeon_ to be clear the one liner was not remotely a serious suggestion, it'd be terrible

@monorail here's my final answer:

let i = 0;
for (
let idx = currentSignal.length;
i < matches[0].length && matches[0][idx] === matches.at(-1)[idx];
idx += i++
);

@monorail anyway I was nice and comfy in my bed and this made me get up to use my computer so every one of my followers better favorite this

@wallhackio @monorail idx should increment by 1 which is not what either of those would do

@wallhackio @aescling if you want to increment by 1 every time, one way i like to do that is with += 1

@aescling @wallhackio by looking up "javascript shared prefix of multiple strings" on google and seeing a stack overflow page about it

@monorail @aescling this thread is justifying my current software unemployment

@vaporeon_ @aescling @monorail yeah that's what i would do (actually I would do ++idx, ++i but it objectively doesn't matter)

@vaporeon_ @aescling @monorail also this thread makes me certain that "I'm not sure I would give enough of a shit in a code review because either way would look Weird" was the right answer all along

@wallhackio @vaporeon_ @aescling sometimes you have an iife that constructs a script tag and injects it into the DOM, that's also conventional (but not always necessary)

@monorail @vaporeon_ @aescling I will support iife usage even if its stupid, iifes give me life

@wallhackio @vaporeon_ @aescling it makes sense for userscripts because they like to pretend they live in their own little bubble but you absolutely can pollute the global namespace with them and cause big bad problems

@monorail @vaporeon_ @aescling my only note is to use matches.at(-1) instead of matches[matches.length-1] at line 53 but even so, your javascript is clod-approved :clodsire_head::clodsire_tail:

@monorail @wallhackio i would pat your head but im tiny and you’re all the way up there

Sign in to participate in the conversation
📟🐱 GlitchCat

A small, community‐oriented Mastodon‐compatible Fediverse (GlitchSoc) instance managed as a joint venture between the cat and KIBI families.