Iterators for D

Sean Kelly sean at f4.ca
Mon Nov 6 14:37:06 PST 2006


rm wrote:
> 
>> I think the Java style may be more appropriate:
>>
>>     foreach( e; s.fwdIter() )
>>     foreach( e; s.revIter() )
>>
> 
> I didn't read the discussion, but wasn't there a topic about 
> foreach_reverse? Should that not cover s.revIter()?

How?  Say I do this:

     foreach_reverse( e; s.begin() )

I've passed an iterator pointing to the beginning of a sequence to an 
algorithm that wants to iterate backwards, so best case the iterator is 
bidirectional and we will visit exactly one element (the theoretical 
"last" in this case) before exiting the loop.

> Secondly, why must one give in the foreach statement the first iterator?
> I'd say that there the iterable must be given?

Something like that, yes.  You need a unified object that knows when 
it's reached the end of the sequence.  So assuming C++ style iterators 
you'd have to wrap them in an object that takes care of the comparisons 
and looping to work with foreach.


Sean



More information about the Digitalmars-d mailing list