std way to remove multiple indices from an array at once
Seb
seb at wilzba.ch
Thu Dec 21 00:48:10 UTC 2017
On Wednesday, 20 December 2017 at 23:01:17 UTC, aliak wrote:
> Hi, is there a way to remove a number of elements from an array
> by a range of indices in the standard library somewhere?
>
> I wrote one (code below), but I'm wondering if there's a better
> way?
>
> Also, can the below be made more efficient?
>
> auto without(T, R)(T[] array, R indices) if (isForwardRange!R
> && isIntegral!(ElementType!R) && !isInfinite!R) {
> T[] newArray;
> ElementType!R start = 0;
> foreach (i; indices) {
> newArray ~= array[start .. i];
> start = i + 1;
> }
> newArray ~= array[start .. $];
> return newArray;
> }
>
> // Usage
> long[] aa = [1, 2, 3, 4]
> aa = aa.without([1, 3])
>
> Thanks!
Try std.algorithm.mutation.remove [1]:
```
import std.algorithm, std.range, std.stdio, std.typecons;
auto r = 10.iota.array;
r.remove(tuple(2, 4)).writeln;
```
Play with it: https://run.dlang.io/is/RcL3VX
[1] https://dlang.org/phobos/std_algorithm_mutation.html#remove
More information about the Digitalmars-d-learn
mailing list