Suppose there are two sequences and I want them to always have the same length in order to match their items later. I want two separate commands for adding items to them. I came up with this:
It results in:
and this is correct considering my need.
Does this look okay? Am I violating any standard conventions? Is there any further optimisation possible?
With the conditions
- that seq A is always assigned first and
- that seq A is guaranteed to have more meaningful entries than seq B (which somewhat follows from the first point)
you can optimise your code a bit:
Alternative methods of input for this syntax could be a key=value based input (though this puts all entries together in a single argument – so doesn’t meet your requirements “I want two separate commands for adding items to them” – nevertheless I think the interface is pretty clean, and quite easy to implement, so here goes nothing):
Output (regardless of whether the key=value or the first interface is used):
If your use case doesn’t guarantee the above two constraints you could still implement something similar to the first variant, just with a state variable (we can’t use a boolean here since there are three states, initial, A, and B – we use an integer with the values 0, 1, and 2 for these):
Output:
Note that \seq_set_item:Nnn <seq-var> { -1 }
is a rather slow operation. If there was a finalising call for the setup block we could further optimise our first idea by introducing a state variable that monitors how many items overhead are already built up (this is close to your code, but a bit faster by simply counting the number of items while we go, instead of every time we add to b
):
Lastly, we can reintroduce the constraint that we don’t know which sequence is added first and which second, and which one contains more items. For that we use positive values to indicate an overhead of A, and negative ones to indicate an overhead of B: