Removing multiple elements from array

aldanor i.s.smirnov at gmail.com
Sat Dec 21 06:06:40 PST 2013


On Saturday, 21 December 2013 at 02:52:00 UTC, bearophile wrote:
> Do not forget to add the () after the sort, otherwise you call 
> the deprecated, buggy and slow built-in sort.
>
> reverse is another deprecated built-in, so use "retro".
>
> The first "if" is not much useful, trying to sort an empty 
> array will not wast much time.
>
> foreach (i; index.sort().retro)
>     a = a.remove(i);
>
> In alternative you can also invert the sorting cmp (untested):
>
> foreach (i; index.sort!q{a > b})
>     a = a.remove(i);

Thanks, that looks like the cleanest solution so far! So (I 
guess) it will not cause index.length reallocations by just 
moving the elements in-place and then returning a slice?

P.S. I wonder where would one learn about the deprecated sort and 
reverse if not from this forum? I followed the official docs and 
there's nothing about these properties being deprecated... yet 
another spot where a compiler warning would be appropriate?


More information about the Digitalmars-d-learn mailing list