D Recurrences

Ben Grabham Evil.Nebster at gmail.com
Thu Jun 9 20:35:18 PDT 2011


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)
	...

Thanks,
Nebster


More information about the Digitalmars-d mailing list