4 ms·
Delimiter-first thinking leads to better code when you need to write a loop to produce some separated list. for (x in items; let comma = false) { if (w
by kazinator 4y ago
Delimiter-first thinking leads to better code when you need to write a loop to produce some separated list.
for (x in items; let comma = false) {
if (want_to_print(x)) {
if (comma)
print ", "
print x
comma = true
}
}
A delimiter-separated list should be regarded as all but the first item being preceded by the delimiter, rather than all but the last item followed by a delimiter.
If you that the latter view, you write silly code which tries to guess whether more items are going to be printed in subsequent iterations, and thus whether to generate a trailing comma now.