www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Do you have a better way to remove element from a array?

reply "FrankLike" <1150015857 qq.com> writes:
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
parent reply "Tobias Pankrath" <tobias pankrath.net> writes:
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
next sibling parent reply "FrankLike" <1150015857 qq.com> writes:
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
parent "Tobias Pankrath" <tobias pankrath.net> writes:
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:

 Works as designed: 

Thank you. aa = remove(aa,1);//ok but how to remove one item? such as aa.remove(2) ?
I don't get your question.
Feb 05 2015
prev sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
next sibling parent "FrankLike" <1150015857 qq.com> writes:
On Thursday, 5 February 2015 at 14:09:10 UTC, bearophile wrote:
 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
Yes,A.remove(item) or A.removeAt(index) They is better than now.
Feb 05 2015
prev sibling next sibling parent "FrankLike" <1150015857 qq.com> writes:
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
prev sibling parent reply "bachmeier" <no spam.net> writes:
On Thursday, 5 February 2015 at 14:09:10 UTC, bearophile wrote:
 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
It seems your argument is that remove is poorly designed because it's not destructive. Or am I missing your argument?
Feb 05 2015
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
parent FG <home fgda.pl> writes:
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