digitalmars.D.learn - problem with filter.
- Knud Soerensen (31/31) Mar 18 2013 Hi
- Andrea Fontana (4/45) Mar 18 2013 First writeln() actually edit the original array, so when you
- Knud Soerensen (1/3) Mar 18 2013 How would you get around this ?
- Andrea Fontana (15/20) Mar 18 2013 You should work on a whole copy of clist array.
Hi I am trying to generate 2 arrays which is modifications of clist. But I am having some problems, how would you do it ? 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
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 ? 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
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
On Monday, 18 March 2013 at 09:15:50 UTC, Knud Soerensen wrote: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 :)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