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;
}

)

@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

@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 I did more thinking about this while I was supposed to be playing Deltarune and am now preferring:

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

@wallhackio @monorail But you never define or increment i before using it in while (i<...), do you? I think this can't work, this'll throw an error

@wallhackio @monorail Maybe you (Clodsire who really wants a while loop here) could do this? Since Holly said she also needs to check the length of the second list, because of longest common prefix.

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

@wallhackio @monorail Personally I, since it isn't performance-critical, would define a function so that I can rename matches[0] and matches.at(-1) to simply a and b. Less annoying to type and to read.
Please correct me if this syntax is wrong in JavaScript, I am not a JavaScript programmer.

function longestPref(a, b, sig) {
let i;
for (i=0; i<a.length && i<b.length; i++)
if (a[i+sig.length] !== b[i+sig.length]) break;
return i;
}

/* ... */
let i = longestPref(matches[0], matches.at(-1), currentSignal);

However, this is more lines of code to produce the same result, so maybe this is bad, unless you have reasons to call longestPref multiple times from different prefixes...
What do you think?

Follow

@vaporeon_ I haven't carefully scrutinized it but I like the approach in principle

Personally I prefer readability over concision as long as the code isn't performance critical (which is most code). So bigger code size is rarely an issue for me

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.