On 10/06/11 05:43, Jonathan M Davis wrote:
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

Sorry, didn't really make that clear.

Inside the foreach, I'm using popFront and front to get the values and then outside, I set the index (_n) back to 0. What I want to do is "save" the _n value and then restore it at the beginning and end of the foreach respectively.

I don't have the code on me atm, but when I get hold of it, I'll post it if you still need it.

Thanks,
Nebster

Reply via email to