foreach(r; requests) { r.concider(); }

Christophe travert at phare.normalesup.org
Fri Nov 4 06:01:11 PDT 2011


"Steven Schveighoffer" , dans le message (digitalmars.D:146563), a
 écrit :

The foreach delegate syntax already allow you to have parametrized 
iteration over a structure !

Actually, the delegate do not have to be returned by the & operator, it 
can be returned by a function. It's a bit awkward first, but then it's 
easy to use:

struct Iterable
{
    int delegate(int delegate(ref int)) inReverse()
    {
	return ()
	{
          int result = 0;
          foreach(int i; 0..100)
          {
             auto t = 99-i;
             if((result = dg(t)) != 0) break;
          }
          return result;
	}
    }

    int delegate(int delegate(ref int)) byStep(int step)
    {
       return()
         {
           int result = 0;
           foreach(int i; iota(0, 100, step))
           {
              auto t = i;
              if((result = dg(t)) != 0) break;
           }
           return result;
         }
    }
}


int main()
{
  Iterable it;
  foreach (i; it.inReverse) writeln(i);
  foreach (i; it.byStep(2)) writeln(i);
}


There is no need to add a special syntax to do what you want !

Maybe there should be a bit more documentation about this.


More information about the Digitalmars-d mailing list