www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - foreach statement: Are there no Iterators in D?

reply J.Frank <joe cerberon.net> writes:
Hello,

I am looking for something like the "Iterator" Interface in Java 
or PHP.
Is there something comparable in D that can be used with foreach?

Thanks,

J.Frank
Nov 08 2015
parent reply Rikki Cattermole <alphaglosined gmail.com> writes:
On 09/11/15 12:40 AM, J.Frank wrote:
 Hello,

 I am looking for something like the "Iterator" Interface in Java or PHP.
 Is there something comparable in D that can be used with foreach?

 Thanks,

 J.Frank
opApply if you want 0 .. N iterations during for a foreach statement and having it reset each time. Otherwise you want ranges :) An input range is more or less an iterator as you would think of it. You only need popFront, front and empty. But here is the "official" interface. http://dlang.org/phobos/std_range_interfaces.html#InputRange You can have structs an an input range, it is quite common since it doesn't allocate.
Nov 08 2015
parent reply J.Frank <joe cerberon.net> writes:
On Sunday, 8 November 2015 at 11:47:41 UTC, Rikki Cattermole 
wrote:
 opApply if you want 0 .. N iterations during for a foreach 
 statement and having it reset each time.
No, that won't help. I want to be able to iterate over a data set of infinite size.
 Otherwise you want ranges :)

 An input range is more or less an iterator as you would think 
 of it.
 You only need popFront, front and empty.
Ah yes, that's what I missed. Looks good. Thank you. :)
Nov 08 2015
parent rsw0x <anonymous anonymous.com> writes:
On Sunday, 8 November 2015 at 11:57:16 UTC, J.Frank wrote:
 On Sunday, 8 November 2015 at 11:47:41 UTC, Rikki Cattermole 
 wrote:
 opApply if you want 0 .. N iterations during for a foreach 
 statement and having it reset each time.
No, that won't help. I want to be able to iterate over a data set of infinite size.
 Otherwise you want ranges :)

 An input range is more or less an iterator as you would think 
 of it.
 You only need popFront, front and empty.
Ah yes, that's what I missed. Looks good. Thank you. :)
FWIW since you mentioned Java, if you're accustomed to Java 8 streams they're very similar to D's ranges.
Nov 08 2015