October 24, 2010
Adam Cigánek wrote:

> Is there a function in the standard library to delete an element from
> an array (or range)?

arrays are computational not well suited for deleting elements, nor are lists.

-manfred
October 24, 2010
Manfred_Nowak:

> arrays are computational not well suited for deleting elements, nor are lists.

On the other hand dynamic arrays are handy for many other purposes. So if you have just 20 items, like some buttons of your GUI, you may want to use a dynamic array to add and remove them, especially if you have handy standard functions/methods to add and remove items. The runtime, even in Python, will never show you any slowdown. Python lists are arrays dynamic to the right, similar to D dynamic arrays, and they are handy.

So computational complexity considerations aren't the only things to keep in account when you write code.

Bye,
bearophile
October 24, 2010
spir:

> In my opinion, such non-obvious complications are good reasons to have seemingly trivial operations implemented as builtin routines. (and should throw error in case of failure)

std.algorithm.delete contains code like if(rEnd == range.length), so if you give it a signed integer coming from indexOf I think D converts it to unsigned... This is not good.

In an enhancement request in Bugzilla you may ask for a function that removes the first instance of an item equal to the given item (if it's not present it may raise an exception or return a boolean false). In your bug report you may link Python docs where it shows both ways to remove an item by index or by value. If you don't want to write this enhancement request, I may do it myself.

Bye,
bearophile
October 24, 2010
On Sun, 24 Oct 2010 14:47:43 -0400
bearophile <bearophileHUGS@lycos.com> wrote:

> Manfred_Nowak:
> 
> > arrays are computational not well suited for deleting elements, nor are lists.

Sequences of all kinds are the worst possible kind of collection for this operation (not only linear search, but shift all elements placed after the one found & removed). But...

> On the other hand dynamic arrays are handy for many other purposes. So if you have just 20 items, like some buttons of your GUI, you may want to use a dynamic array to add and remove them, especially if you have handy standard functions/methods to add and remove items. The runtime, even in Python, will never show you any slowdown. Python lists are arrays dynamic to the right, similar to D dynamic arrays, and they are handy.
> 
> So computational complexity considerations aren't the only things to keep in account when you write code.

... Agreed. Typically, remove and such costly ops happen once in a while, when indexing, traversal, slicing for which sequences are handy and super-efficient happen all the time.

Bearophile, what do you mean with "arrays dynamic to the right"? (that they extend/compress (only) on the right side?)

> Bye,
> bearophile


Denis
-- -- -- -- -- -- --
vit esse estrany ☣

spir.wikidot.com

October 24, 2010
> Your new example doesn't show it better, it's the only one you've given that shows it at all.  What you had originally was
>
>  auto a = [1, 2, 3, 4, 5, 6];
>  auto b = delete(a, 4);
>
>  assert([1, 2, 3, 4, 6] == b);
>
> which shows the removal of the element at index 4, not the element with value 4.

Yep, I made a typo. The last line was supposed to be
assert([1,2,3,5,6] == b). My bad.

>
> But there are further questions to be answered:
>
> - What do you want it to do if the value isn't in the array?  Just return the original array, return a copy of the original array, or throw an exception?

Good question. I would return the array unchanged (or maybe returning range pointing to the same data as the original array). The justification is that since I'm going to delete the element anyway, I probably "don't care" about it, so it does not matter if it's in the array in the first place. Throwing an exception does not seem useful to me.

>
> - What do you want it to do if the value is in the array more than once?  Remove the first, remove the last, remove all of them, or pick one at random?

Another good question. In the end, there should probably be more functions: deleteFirst, deleteLast, deleteAll or even deleteRandom.

adam.

>
> Stewart.
>
October 24, 2010
spir:

> Bearophile, what do you mean with "arrays dynamic to the right"? (that they extend/compress (only) on the right side?)

In Python you may add items at the start too of a list, but that's not an efficient operation, Python lists are amortized efficient only if you append items to their right side, because they may keep some free space only on their right site. In Python the deque collection supports efficient add/remove from both ends.

Bye,
bearophile
October 25, 2010
bearophile wrote:

> some buttons of your GUI

I doubt that one wants to _delete_ buttons of a GUI instead of inactivating them and the OP did ask for "a function in the standard library to delete an element from an array", i.e. arrays without any restrictions on the number of elements as supposed by you.

-manfred
October 26, 2010
On Sun, 24 Oct 2010 13:02:24 +0200, Adam Cigánek wrote:

> Hello,
> 
> Is there a function in the standard library to delete an element from an array (or range)? Something like:
> 
>   auto a = [1, 2, 3, 4, 5, 6];
>   auto b = delete(a, 4);
> 
>   assert([1, 2, 3, 4, 6] == b);
> 
> I've noticed there is eliminate in std.algorithm, which seems to be doing just that, but it's commented out.
> 
> It's not difficult to roll my own, (with the help of indexOf and remove), but I thought that this is pretty common operation so it should be in the standard library.
> 
> 
In D2 std.array has

void replace(T, Range)(ref T[] array, size_t from, size_t to, Range stuff);
	and a delete can be done by passing null as stuff.

This multipurpose template will replace with whatever gets passed as stuff, including null.
import std.array;
...
auto a = [1, 2, 3, 4, 5, 6];
replace(a, 4, 5, null);    // delete slice 4..5
assert([1, 2, 3, 4, 6] == a);


The documentation also shows comments for code that has been commented out. eg Erases element from array at index from.  (code is commented out).

Shows a need for doc comments to be commented out with the code they refer to, as this confused me for a bit, until I found that the 4 argument version is the only working one.



1 2
Next ›   Last »