On Monday, 18 November 2019 at 21:25:12 UTC, kerdemdemir wrote:
It is a bit weird that such a general case like removing list
of elements does not work.
And I really do not know the real use of
[0,1,2,3].remove(1,2,3). I mean in unit test it looks cool but
in real life you will have dynamically calculated indexes.
Erdem
It seems like a defect in phobos to me as well. Fortunately phobos
is written in D so you can just copy&paste this into your code:
https://github.com/dlang/phobos/blob/master/std/algorithm/mutation.d#L2160
however, it still doesn't return the same thing as
x.remove(1,2,3).
... maybe it'd be simpler to drop down to C-style D?
// no checks for out-of-bounds or unsorted offsets
void myRemove(ref int[] array, int[] offsets) {
int from, to, offset;
while (from < array.length) {
if (offset < offsets.length && from == offsets[offset]) {
++offset;
++from;
} else if (from == to) {
++to;
++from;
} else {
array[to++] = array[from++];
}
}
array.length = to;
}
unittest {
int[] x = [1, 2, 3];
int[] y = x.dup, z = x.dup, q = x.dup;
myRemove(x, [0]);
myRemove(y, []);
myRemove(z, [2]);
myRemove(q, [1, 2]);
assert(x == [2, 3]);
assert(y == [1, 2, 3]);
assert(z == [1, 2]);
assert(q == [1]);
}
Perhaps it's nice to have the option?