Why is std.algorithm so complicated to use?

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sun Jul 15 15:22:45 PDT 2012


On 7/15/12 5:42 PM, Mehrdad wrote:
> Another example of how std.algorithm is so hard to use (it's almost
> tempting me to start swearing...):
>
> How do you remove an item from an array in place?
>
> It seems so darn simple, and yet it's not in std.algorithm (or
> std.range). It makes something so easy so tedious.

Look for "remove" here:

http://dlang.org/phobos/std_algorithm.html

Unfortunately the anchor called "remove" is swallowed by an unrelated 
enum value (we should fix that in ddoc).

 From the example:

int[] a = [ 3, 5, 7, 8 ];
assert(remove(a, 1) == [ 3, 7, 8 ]);
assert(a == [ 3, 7, 8, 8 ]);

Therefore, to remove an element in place:

int[] a = [ 3, 5, 7, 8 ];
a = remove(a, 1);

You also have a less stable but faster version:

a = remove!(SwapStrategy.unstable)(a, 1);


Andrei


More information about the Digitalmars-d mailing list