Iterators for D
Bill Baxter
dnewsgroup at billbaxter.com
Mon Nov 6 16:12:46 PST 2006
Walter Bright wrote:
> Tom S wrote:
>> Walter Bright wrote:
>>> foreach loops will not be able to have a 'key' parameter.
>> What's the particular reason for that restriction ?
>
> With an iterator, what is the key?
It could just be the count. Say I'm trying to linearize a non-linear
datastructure into a pre-existing array:
foreach(int i, val v; hierarchical_thing.iterator())
{
linearized[i] = val;
}
Also having the count there would make it easier to do something like
iterating lock step in funky ways over different objects:
foreach(int i, val v; thing.randomized_iterator()) {
other_thing[i] = val;
}
Obviously you could count things yourself if you had to.
But I find for instance in Python that I use 'enumerate()' quite often.
enumerate() takes an iterator and returns an iterator that spits out
(count,value) tuples.
--bb
More information about the Digitalmars-d
mailing list