Reducing an array

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Apr 19 10:12:05 PDT 2014


On 04/19/2014 09:55 AM, MattCoder wrote:

 > On Friday, 18 April 2014 at 20:02:41 UTC, Tim Holzschuh via
 > Digitalmars-d-learn wrote:
 > void main(){
 >      int myfilter(int a){
 >          static int[] b;

That static variable makes this solution non-reentrant. To see an effect 
of this, replace the arrFiltered line with the following:

     import std.range;
     auto arr2 = [1,1,5,2,3];
     auto arrFiltered = zip(arr.filter!(a => myfilter(a) > 0),
                            arr2.filter!(a => myfilter(a) > 0));

Now the two filter operations get in each other's way and the output 
becomes:

     [Tuple!(int, int)(1, 5), Tuple!(int, int)(2, 3)]

I wonder what happened to 4. (?) :)

 >          if(b.find(a) == []){

There is a more explicit way of saying that:

         if(!b.canFind(a)){

Ali



More information about the Digitalmars-d-learn mailing list