digitalmars.D.learn - Do you have a better way to remove element from a array?
- FrankLike (16/16) Feb 05 2015 Now I can remove element from a array:
- Tobias Pankrath (3/19) Feb 05 2015 Works as designed:
- FrankLike (6/8) Feb 05 2015 Thank you.
- Tobias Pankrath (2/10) Feb 05 2015 I don't get your question.
- bearophile (5/7) Feb 05 2015 Unfortunately it's one of the worst designed functions of Phobos:
- FrankLike (3/11) Feb 05 2015 Yes,A.remove(item) or A.removeAt(index)
- FrankLike (3/3) Feb 05 2015 On Thursday, 5 February 2015 at 14:09:10 UTC, bearophile wrote:
- bachmeier (3/11) Feb 05 2015 It seems your argument is that remove is poorly designed because
- bearophile (9/11) Feb 05 2015 It has to be a void function (or perhaps bettter it can return
- FG (3/6) Feb 05 2015 I was never a fan of STL's erase-remove idiom, although the decoupling o...
- mw (12/20) Aug 10 Hit this one today,
- Lance Bachmeier (4/27) Aug 11 You may want to create a new thread about this in General. If
- IchorDev (5/14) Aug 11 One of them is part of DRuntime, and the other is a function you
- aberba (2/20) Aug 21 Naa, inconsistency is a bad design.
- IchorDev (4/25) Aug 21 Do hate value types for not being consistent with reference types
- IchorDev (10/13) Aug 11 I was looking through Phobos to find what might be an appropriate
Now I can remove element from a array: module removeOne; import std.stdio; import std.array; import std.algorithm; void main() { int[] aa =[1,2,3,4,5]; aa = aa[0..2] ~aa[3..$]; writeln(aa); //ok remove(aa,1); writeln(aa);//get error result } You will found the error result,why? Thank you.
Feb 05 2015
On Thursday, 5 February 2015 at 13:25:37 UTC, FrankLike wrote:Now I can remove element from a array: module removeOne; import std.stdio; import std.array; import std.algorithm; void main() { int[] aa =[1,2,3,4,5]; aa = aa[0..2] ~aa[3..$]; writeln(aa); //ok remove(aa,1); writeln(aa);//get error result } You will found the error result,why? Thank you.Works as designed:
Feb 05 2015
On Thursday, 5 February 2015 at 13:29:30 UTC, Tobias Pankrath wrote:Works as designed:Thank you. aa = remove(aa,1);//ok but how to remove one item? such as aa.remove(2) ?
Feb 05 2015
On Thursday, 5 February 2015 at 13:55:59 UTC, FrankLike wrote:On Thursday, 5 February 2015 at 13:29:30 UTC, Tobias Pankrath wrote:I don't get your question.Works as designed:Thank you. aa = remove(aa,1);//ok but how to remove one item? such as aa.remove(2) ?
Feb 05 2015
Tobias Pankrath:Works as designed:Unfortunately it's one of the worst designed functions of Phobos: https://issues.dlang.org/show_bug.cgi?id=10959 Bye, bearophile
Feb 05 2015
On Thursday, 5 February 2015 at 14:09:10 UTC, bearophile wrote:Tobias Pankrath:Yes,A.remove(item) or A.removeAt(index) They is better than now.Works as designed:Unfortunately it's one of the worst designed functions of Phobos: https://issues.dlang.org/show_bug.cgi?id=10959 Bye, bearophile
Feb 05 2015
On Thursday, 5 February 2015 at 14:09:10 UTC, bearophile wrote: Yes,A.remove(item) or A.removeAt(index) They are better than now.
Feb 05 2015
On Thursday, 5 February 2015 at 14:09:10 UTC, bearophile wrote:Tobias Pankrath:It seems your argument is that remove is poorly designed because it's not destructive. Or am I missing your argument?Works as designed:Unfortunately it's one of the worst designed functions of Phobos: https://issues.dlang.org/show_bug.cgi?id=10959 Bye, bearophile
Feb 05 2015
bachmeier:It seems your argument is that remove is poorly designed because it's not destructive. Or am I missing your argument?It has to be a void function (or perhaps bettter it can return true/false if it has removed the item, so it becomes nogc and nothrow). And it has to remove the first item equal to the given one. You can then add a second function that removes at a given index (like removeAt). Bye, bearophile
Feb 05 2015
On 2015-02-05 at 17:25, bearophile wrote:It has to be a void function (or perhaps bettter it can return true/false if it has removed the item, so it becomes nogc and nothrow). And it has to remove the first item equal to the given one. You can then add a second function that removes at a given index (like removeAt).I was never a fan of STL's erase-remove idiom, although the decoupling of algorithms and containers is in general a great idea. However, in D algorithms can be smarter, because they operate on ranges instead of iterators. I don't see why remove has to follow the C++ example. Therefore I have to ask: Is there any reason why `remove` doesn't take the range by reference and `popBack` as many elements as were removed?
Feb 05 2015
On Thursday, 5 February 2015 at 14:09:10 UTC, bearophile wrote:Tobias Pankrath:Hit this one today, Has `removeAt` or `removeAtIndex` be added to the std lib? BTW, for associative array, `remove()` is in-place; but here for std.algorithm.mutation.remove (*keyword*: mutation), one need to do ``` array = array.remove(index); // return a new container // v.s. aa.remove(key); // return bool (if it's removed) ``` This in-consistence is really bad.Works as designed:Unfortunately it's one of the worst designed functions of Phobos: https://issues.dlang.org/show_bug.cgi?id=10959 Bye, bearophile
Aug 10
On Sunday, 11 August 2024 at 06:04:08 UTC, mw wrote:On Thursday, 5 February 2015 at 14:09:10 UTC, bearophile wrote:You may want to create a new thread about this in General. If it's going to get changed, now is the time as they're working on updates to Phobos, but they're not going to see this post.Tobias Pankrath:Hit this one today, Has `removeAt` or `removeAtIndex` be added to the std lib? BTW, for associative array, `remove()` is in-place; but here for std.algorithm.mutation.remove (*keyword*: mutation), one need to do ``` array = array.remove(index); // return a new container // v.s. aa.remove(key); // return bool (if it's removed) ``` This in-consistence is really bad.Works as designed:Unfortunately it's one of the worst designed functions of Phobos: https://issues.dlang.org/show_bug.cgi?id=10959 Bye, bearophile
Aug 11
On Sunday, 11 August 2024 at 06:04:08 UTC, mw wrote:BTW, for associative array, `remove()` is in-place; but here for std.algorithm.mutation.remove (*keyword*: mutation), one need to do ``` array = array.remove(index); // return a new container // v.s. aa.remove(key); // return bool (if it's removed) ``` This in-consistence is really bad.One of them is part of DRuntime, and the other is a function you can optionally import from Phobos. The fact that they do not work the same way is not necessarily bad—they’re from completely different places and serve completely different purposes.
Aug 11
On Sunday, 11 August 2024 at 20:57:15 UTC, IchorDev wrote:On Sunday, 11 August 2024 at 06:04:08 UTC, mw wrote:Naa, inconsistency is a bad design.BTW, for associative array, `remove()` is in-place; but here for std.algorithm.mutation.remove (*keyword*: mutation), one need to do ``` array = array.remove(index); // return a new container // v.s. aa.remove(key); // return bool (if it's removed) ``` This in-consistence is really bad.One of them is part of DRuntime, and the other is a function you can optionally import from Phobos. The fact that they do not work the same way is not necessarily bad—they’re from completely different places and serve completely different purposes.
Aug 21
On Wednesday, 21 August 2024 at 08:00:04 UTC, aberba wrote:On Sunday, 11 August 2024 at 20:57:15 UTC, IchorDev wrote:Do hate value types for not being consistent with reference types then? That’s the main difference between the two: slices are values and associative arrays are references.On Sunday, 11 August 2024 at 06:04:08 UTC, mw wrote:Naa, inconsistency is a bad design.BTW, for associative array, `remove()` is in-place; but here for std.algorithm.mutation.remove (*keyword*: mutation), one need to do ``` array = array.remove(index); // return a new container // v.s. aa.remove(key); // return bool (if it's removed) ``` This in-consistence is really bad.One of them is part of DRuntime, and the other is a function you can optionally import from Phobos. The fact that they do not work the same way is not necessarily bad—they’re from completely different places and serve completely different purposes.
Aug 21
On Sunday, 11 August 2024 at 06:04:08 UTC, mw wrote:``` array = array.remove(index); // return a new container ```I was looking through Phobos to find what might be an appropriate replacement for `remove`. I read `remove`’s documentation, and there it says it removes the element in-place, so I checked the implementation and that seems to be true. It moves everything over and then returns the array with the last element sliced off. So, for removing an element from the start of the array you’d be better off using slicing (because it just changes a pointer rather than doing a huge copy) but otherwise it’s pretty efficient.
Aug 11