D Recurrences

Jonathan M Davis jmdavisProg at gmx.com
Thu Jun 9 21:43:06 PDT 2011


On 2011-06-09 20:35, Ben Grabham wrote:
> On 09/06/11 20:15, Jonathan M Davis wrote:
> > The save property of a forward range returns a copy of that range. In
> > most cases, since ranges are generally restructs, it just returns the
> > range. You use it when you want to save the original range and still be
> > able pop elements off.
> > 
> > auto orig = range.save;
> > 
> > //pop off as many elements from range as I want.
> > //orig still has all of its elements
> > 
> > The elements aren't copied however, just the range. Regardless, it has
> > nothing to do with returning an element from a range, so I don't
> > understand your question about returning an index rather than a whole
> > object. Ranges don't really use indicies. indexOf will tell you the
> > index of a particular element, and you can increment a counter every
> > time you pop off an element if you want to know how many elements you've
> > consumed, but once an element has been popped off, it's not part of that
> > range anymore (though it could be part of a saved range), so that could
> > seriously affect by what you mean by index, depending on what you're
> > doing.
> > 
> > - Jonathan M Davis
> 
> I want to make it so that foreach works but I'm storing an array which
> when changed, want to keep it like that, even after the foreach, I just
> want to reset the index to 0
> 
> At the moment, I have to do:
> foreach(i; 0..100)
> 	...
> 
> lazy._n = 0;
> 
> foreach(i; 0..100)
> 	...
> 
> I want to do:
> foreach(n; lazy)
> 	...
> foreach(n; lazy)
> 	...

0 .. 100 has nothing to do with ranges. That's a built-in feature of foreach. 
This would use a range

foreach(i; iota(0, 100))

because iota generates one, but 0 .. 100 doesn't. But there's no array 
involved in

foreach(i; 0 .. 100)

anyway, and you state that you're storing an array as part of this, so I 
really don't know what you're trying to do. I'd need to see actual code to be 
of any help.

- Jonathan M Davis


More information about the Digitalmars-d mailing list