problem with filter.
Andrea Fontana
nospam at example.com
Mon Mar 18 01:38:46 PDT 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 ?
>
>
> #!/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.
More information about the Digitalmars-d-learn
mailing list