D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6849 - std.algorithm.remove design
Summary: std.algorithm.remove design
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: x86 Windows
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-10-25 04:21 UTC by bearophile_hugs
Modified: 2011-11-15 16:57 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2011-10-25 04:21:13 UTC
A D2 program:

import std.stdio, std.algorithm;
void main() {
    dchar[] data = "abcde"d.dup;
    writeln(data);
    writeln(remove(data, 0));
    writeln(data);
    writeln(remove(data, 2));
    writeln(data);
}


With DMD 2.056beta3 it prints:

abcde
bcde
bcdee
bcee
bceee


Expected output:

abcde
bcde
bcde
bce
bce


Or maybe it's better for remove() to return void, to underline its in-place nature (this is often done in Python, where functions the modify da in-place usually return just None, and functions with a name that ends with "ed" create new data and return it, like the sort/sorted pair):


import std.stdio, std.algorithm;
void main() {
    dchar[] data = "abcde"d.dup;
    writeln(data);
    data.remove(0);
    writeln(data);
    data.remove(2);
    writeln(data);
}


Expected output:

abcde
bcde
bce


This is similar to what happens in Python2:

>>> data = ['a', 'b', 'c', 'd', 'e']
>>> del data[0]
>>> data
['b', 'c', 'd', 'e']
>>> del data[2]
>>> data
['b', 'c', 'e']
Comment 1 bearophile_hugs 2011-10-25 16:27:57 UTC
I didn't fully understand the situation, as it often happens:

http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=30277

Yet I think the problem exists, and there are Python (and other languages) programmers too coming to D. See some comments, with an alternative proposal too, about a replaceInPlace() function:

http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=30281
Comment 2 bearophile_hugs 2011-10-26 10:14:57 UTC
From the same thread:

http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=30294

Steven Schveighoffer:

> Here is the resulting array and final state of a:
>
> import std.stdio, std.algorithm;
> void main() {
>      int[] a = [ 0, 1, 2, 3 ];
>      writeln( remove!(SwapStrategy.unstable)(a, 1));
>      writeln(a);
> }
> 
> output:
> 
> [3, 1, 2]
> [3, 1, 2, 3]
> 
> Clearly, index 0 was removed, not index 1.  Please file a bug.
Comment 3 Steven Schveighoffer 2011-10-26 10:25:44 UTC
(In reply to comment #2)
> From the same thread:
> 
> http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=30294
> 
> Steven Schveighoffer:
> 
> > 
> > Clearly, index 0 was removed, not index 1.  Please file a bug.

This is not a design bug, it's an actual bug in the code (does not work as designed), it should be separate from this enhancement request.
Comment 4 bearophile_hugs 2011-10-26 15:07:02 UTC
(In reply to comment #3)
> (In reply to comment #2)
> > From the same thread:
> > 
> > http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=30294
> > 
> > Steven Schveighoffer:
> > 
> > > 
> > > Clearly, index 0 was removed, not index 1.  Please file a bug.
> 
> This is not a design bug, it's an actual bug in the code (does not work as
> designed), it should be separate from this enhancement request.

Right, it's my mistake of mixing two different things in the same issue. Sorry. I'll open another issue. Please ignore Comment #2.
Comment 5 bearophile_hugs 2011-11-15 16:57:35 UTC
To clear up this bug report I close it and replace it with a a bug report and a more focused enhancement request.

See bug 6956 instead of Comment #2.

See also bug 6957.