www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - problem with filter.

reply Knud Soerensen <4tuu4k002 sneakemail.com> writes:
Hi

I am trying to generate 2 arrays which is modifications of clist.
But I am having some problems, how would you do it ?


#!/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("pos:",map!(delegate (bool[][] a){a[x][0]=true; return
a;})(filter!(fpos)(clist)));
 writeln("neg:",map!(delegate (bool[][] a){a[x][1]=true; return
a;})(filter!(fneg)(clist)));


}
The code outputs:

[[[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]]]

But I should be

[[[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]]]

Hope you can help.
Mar 18 2013
parent reply "Andrea Fontana" <nospam example.com> writes:
On Monday, 18 March 2013 at 07:27:21 UTC, Knud Soerensen wrote:
 Hi

 I am trying to generate 2 arrays which is modifications of 
 clist.
 But I am having some problems, how would you do it ?


 #!/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("pos:",map!(delegate (bool[][] a){a[x][0]=true; return
 a;})(filter!(fpos)(clist)));
  writeln("neg:",map!(delegate (bool[][] a){a[x][1]=true; return
 a;})(filter!(fneg)(clist)));


 }
 The code outputs:

 [[[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]]]

 But I should be

 [[[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]]]

 Hope you can help.
First writeln() actually edit the original array, so when you filter it for the second array, fneg gives true for all clist[][][] elements.
Mar 18 2013
parent reply Knud Soerensen <4tuu4k002 sneakemail.com> writes:
 First writeln() actually edit the original array, so when you filter it
 for the second array, fneg gives true for all clist[][][] elements.
How would you get around this ?
Mar 18 2013
parent "Andrea Fontana" <nospam example.com> writes:
On Monday, 18 March 2013 at 09:15:50 UTC, Knud Soerensen wrote:
 First writeln() actually edit the original array, so when you 
 filter it
 for the second array, fneg gives true for all clist[][][] 
 elements.
How would you get around this ?
You should work on a whole copy of clist array. Or return copy of elements from map function instead of the element itself (<-- faster and lazy solution, i guess) To get a whole copy you can try to do something like this: T recursiveDup(T)(ref T array) if (isArray!T) { T retVal = array.dup; foreach(ref e; retVal) static if (isArray!(typeof(e))) e = recursiveDup(e); return retVal; } gives you a copy of your array. It's a simple code, that works just for your specific case - not so efficient :)
Mar 18 2013