Strange behavior of the function find() and remove()

anonymous@example.com via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Mar 8 15:01:07 PDT 2015


On Sunday, 8 March 2015 at 21:34:25 UTC, Dennis Ritchie wrote:
> This is normal behavior?
>
> import std.stdio;
> import std.algorithm;
>
> void main() {
>
> 	auto a = [3, 5, 8];
>
> 	writeln(find(remove(a, 1), 5).length != 0);     // prints false
> 	writeln(a);		// prints [3, 8, 8] ???
> }

Yes, works as designed. `remove` writes over removed slots and 
returns shrunk (shrinked?) slice. It does not shrink the range at 
the call site. To update `a`, write the result of `remove` to it:

writeln(find(a = remove(a, 1), 5).length != 0); // still false
writeln(a); // prints [3, 8]


More information about the Digitalmars-d-learn mailing list