Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
April 30, 2015 Example from d-idioms is incorrect | ||||
---|---|---|---|---|
| ||||
I was looking at the d-idioms website today and saw this code example: http://p0nce.github.io/d-idioms/#Adding-or-removing-an-element-from-arrays And I was kind of irked. I just recently working with removing an element from an array in a small project I worked on two weeks ago, and I had to learn the hard way that to properly remove an element from an array in the way this example describes, you have to do array.length--; as well. This code: import std.stdio, std.algorithm; void main() { int[] arr; //ensuring it's dynamic arr ~= 1; arr ~= 5; arr ~= 10; arr.remove(1); writeln(arr); writeln(arr == [1, 10]); arr.length--; writeln(arr); writeln(arr == [1, 10]); } produces this output: [1, 10, 10] false [1, 10] true Compiled and ran on Windows, dmd v2.067.0. Unless I'm totally missing something here, that website is giving some pretty wrong information... Was the behavior of the remove() function changed recently? Thanks guys. |
April 30, 2015 Re: Example from d-idioms is incorrect | ||||
---|---|---|---|---|
| ||||
Posted in reply to TheGag96 | On Thursday, 30 April 2015 at 21:30:36 UTC, TheGag96 wrote: > I was looking at the d-idioms website today and saw this code example: > > http://p0nce.github.io/d-idioms/#Adding-or-removing-an-element-from-arrays > > And I was kind of irked. I just recently working with removing an element from an array in a small project I worked on two weeks ago, and I had to learn the hard way that to properly remove an element from an array in the way this example describes, you have to do array.length--; as well. > > This code: > > import std.stdio, std.algorithm; > > void main() { > int[] arr; //ensuring it's dynamic > arr ~= 1; > arr ~= 5; > arr ~= 10; > arr.remove(1); > writeln(arr); > writeln(arr == [1, 10]); > arr.length--; > writeln(arr); > writeln(arr == [1, 10]); > } > > produces this output: > > [1, 10, 10] > false > [1, 10] > true > > Compiled and ran on Windows, dmd v2.067.0. Unless I'm totally missing something here, that website is giving some pretty wrong information... Was the behavior of the remove() function changed recently? Thanks guys. http://dpaste.dzfl.pl/007a9319371d Application output: [1, 10] true [1] false |
April 30, 2015 Re: Example from d-idioms is incorrect | ||||
---|---|---|---|---|
| ||||
Posted in reply to TheGag96 | On Thursday, 30 April 2015 at 21:30:36 UTC, TheGag96 wrote: > arr.remove(1); You didn't assign the result here... the site says arr = arr.remove(index); Notice too that the docs do NOT take the array by reference; they don't necessarily modify it in-place. http://dlang.org/phobos/std_algorithm_mutation.html#.remove So the assignment is important to have. |
April 30, 2015 Re: Example from d-idioms is incorrect | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namespace | Same output: [1, 10] true [1] false with dmd 2.067.1 |
April 30, 2015 Re: Example from d-idioms is incorrect | ||||
---|---|---|---|---|
| ||||
Posted in reply to TheGag96 | On Thu, 30 Apr 2015 21:30:34 +0000, TheGag96 wrote: > Was the behavior of the remove() function changed recently? Thanks guys. I believe remove has always worked this way. What you're seeing is explained by this note in the documentation for remove: > The original array has remained of the same length because all functions in std.algorithm only change content, not topology. The value 8 is repeated because std.algorithm.move was invoked to move elements around and on integers move simply copies the source to the destination. To replace a with the effect of the removal, simply assign a = remove(a, 1). The slice will be rebound to the shorter array and the operation completes with maximal efficiency. |
April 30, 2015 Re: Example from d-idioms is incorrect | ||||
---|---|---|---|---|
| ||||
Posted in reply to Justin Whear | On Thursday, 30 April 2015 at 22:01:43 UTC, Justin Whear wrote:
> On Thu, 30 Apr 2015 21:30:34 +0000, TheGag96 wrote:
>> Was the behavior of the remove() function changed recently? Thanks guys.
>
> I believe remove has always worked this way. What you're seeing is
> explained by this note in the documentation for remove:
>
>> The original array has remained of the same length because all
>> functions in std.algorithm only change content, not topology. The value 8 is repeated because std.algorithm.move was invoked to move elements around and on integers move simply copies the source to the destination. To replace a with the effect of the removal, simply assign a = remove(a, 1). The slice will be rebound to the shorter array and the operation completes with maximal efficiency.
Ah... This is all a lot clearer now. Thanks guys!
|
Copyright © 1999-2021 by the D Language Foundation