Why is std.algorithm so complicated to use?

Jacob Carlborg doob at me.com
Mon Jul 16 00:07:06 PDT 2012


On 2012-07-16 00:03, Jonathan M Davis wrote:

> 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.

It would be really useful if std.algorithm (and similar functions) would 
indicate clearly if they operate in place or not. It took me a while to 
understand that "sort" operates in place. In Ruby this is achieved by a 
naming convention:

a = [3, 4, 5]
a = a.sort # returns a new array
a.sort! # sorts in place

-- 
/Jacob Carlborg




More information about the Digitalmars-d mailing list