@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?!
@vaporeon_ @wallhackio it's advancing an index while those two mismatch
@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
@vaporeon_ @alas @wallhackio you dropped the i increment
@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.
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_ @wallhackio @alas i could but that's a lot of meaning to put on one line for me
@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 probably should be ++i. oh well!
@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 Next time, consider using laptop computer while in bed
@wallhackio @monorail idx should increment by 1 which is not what either of those would do
@wallhackio @monorail no.
@wallhackio @monorail it would increment by 1, then 2, then 3…
@wallhackio @aescling if you want to increment by 1 every time, one way i like to do that is with += 1
@monorail @wallhackio damn holly how did you get so wise
@aescling @wallhackio by looking up "javascript shared prefix of multiple strings" on google and seeing a stack overflow page about it
@aescling @wallhackio oh sorry i thought this was a reply elsewhere
@aescling @wallhackio this one is a holly original wisdom
@monorail @wallhackio that is a good wisdom
@monorail @wallhackio baaaaall-eevee or not, i’m walking on air
@wallhackio @aescling @monorail Can you do something like idx++, i++?
@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 mr. wallhackio do you like my javascript https://git.hollymcfarland.com/monorail/DSCRTC/src/branch/main/dscrtc.user.js#bypass=true
@monorail @vaporeon_ @aescling ooooh yummy delicious iife
@wallhackio @vaporeon_ @aescling this is convention for userscripts you see
@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
@wallhackio @monorail @vaporeon_ please tell me you OiI’d that Iife
@wallhackio @monorail @vaporeon_ he didn’t :c
@aescling @wallhackio @vaporeon_ i have no idea what this means sadly
@monorail @aescling @vaporeon_ welcome to todays lucky 10000 https://www.youtube.com/watch?v=E3p_Crhi3K8
@wallhackio @aescling @vaporeon_ holy shit
sorry clodboy i’m sniping you on this one
re: sorry clodboy i’m sniping you on this one
@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
@wallhackio @vaporeon_ @aescling hooray hooray
i might fix that if i ever commit to this repo again
@wallhackio @aescling i'm less than sure about this
@wallhackio this was mine and it's good enough that i'm not going to make another commit lmao https://git.hollymcfarland.com/monorail/DSCRTC/src/commit/00e6045482dee54286893aeeb01695530251db25/dscrtc.user.js#L46-L55
@monorail @wallhackio link 404s?
@aescling @wallhackio fuuuck
@aescling @wallhackio try now
@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