digitalmars.D.learn - Example from d-idioms is incorrect
- TheGag96 (31/31) Apr 30 2015 I was looking at the d-idioms website today and saw this code
- Namespace (7/38) Apr 30 2015 http://dpaste.dzfl.pl/007a9319371d
- Namespace (6/6) Apr 30 2015 Same output:
- Adam D. Ruppe (7/8) Apr 30 2015 You didn't assign the result here... the site says
- Justin Whear (3/11) Apr 30 2015 I believe remove has always worked this way. What you're seeing is
- TheGag96 (2/16) Apr 30 2015 Ah... This is all a lot clearer now. Thanks guys!
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.
Apr 30 2015
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
Apr 30 2015
Same output: [1, 10] true [1] false with dmd 2.067.1
Apr 30 2015
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. So the assignment is important to have.
Apr 30 2015
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.
Apr 30 2015
On Thursday, 30 April 2015 at 22:01:43 UTC, Justin Whear wrote:On Thu, 30 Apr 2015 21:30:34 +0000, TheGag96 wrote:Ah... This is all a lot clearer now. Thanks guys!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.
Apr 30 2015