Why is std.algorithm so complicated to use?

Jonathan M Davis jmdavisProg at gmx.com
Sun Jul 15 15:03:22 PDT 2012


On Sunday, July 15, 2012 23:42:29 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.

Iterators have exactly the same problem for exactly the same reasons as 
std.algorithm.remove (e.g. take a look at C++'s erase function). The only way 
to do this in place would be to create a separate removeInPlace function 
specifically for arrays. But since all it takes is reassigning the result of 
std.algorithm.remove to the array that you passed in and an 
std.array.replaceInPlace would be doing exactly that, I don't think that 
adding such a function buys us much:

    auto arr = [10, 22, 19, 4, 6];
    arr = remove(arr, 3);
    assert(arr == [10, 22, 19, 6]);

The main problem is understanding why remove (or erase in C++) works this way, 
which seems to throw off a bunch of people in both D and C++, but it's 
something that we're pretty much stuck with. You need the actual container 
(not an iterator or range) if you want to actually remove the element.

- Jonathan M Davis


More information about the Digitalmars-d mailing list