Bug 9740 – strange interaction between map and filter

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
x86
OS
All
Creation time
2013-03-17T01:16:24Z
Last change time
2019-08-09T06:43:20Z
Assigned to
No Owner
Creator
Knud

Comments

Comment #0 by knud — 2013-03-17T01:16:24Z
#!/usr/bin/rdmd import std.stdio; import std.algorithm; void main() { bool[][][] clist=[[[true, true], [false, true]],[[true, false], [true, true]],[[false, true], [true, true]]]; auto x=0; writeln(clist); auto fpos=delegate bool(bool[][] a){return(a[x]!=[true,false]);}; auto fneg=delegate bool(bool[][] a){return(a[x]!=[false,true]);}; writeln("neg:",map!(delegate (bool[][] a){a[x][1]=true; return a;})(filter!(fneg)(clist))); writeln("pos:",map!(delegate (bool[][] a){a[x][0]=true; return a;})(filter!(fpos)(clist))); } outputs: [[[true, true], [false, true]], [[true, false], [true, true]], [[false, true], [true, true]]] neg:[[[true, true], [false, true]], [[true, true], [true, true]]] pos:[[[true, true], [false, true]], [[true, true], [true, true]], [[true, true], [true, true]]] if you change the order of the writeln you get:[[[true, true], [false, true]], [[true, false], [true, true]], [[false, true], [true, true]]] pos:[[[true, true], [false, true]], [[true, true], [true, true]]] neg:[[[true, true], [false, true]], [[true, true], [true, true]], [[true, true], [true, true]]]
Comment #1 by maxim — 2013-03-17T01:39:12Z
What exactly is wrong? Removed OS specific since same happens on windows.
Comment #2 by knud — 2013-03-17T01:46:20Z
(In reply to comment #1) > What exactly is wrong? The second filter don't work, but it should. > > Removed OS specific since same happens on windows.
Comment #3 by simen.kjaras — 2019-08-09T06:43:20Z
The map function changes the contents of the array, and unsurprisingly, that changes what the filter filters. This is not a bug in filter and map, but in your code. Reduced example: auto arr = [[false]]; writeln(arr.filter!(a => a[0]).map!(a => a[0] = true)); // [] writeln(arr.filter!(a => !a[0]).map!(a => a[0] = true)); // [true] writeln(arr.filter!(a => a[0]).map!(a => a[0] = true)); // [false] This is possibly caused by a misguided idea that the array is a value type.