D Recurrences

Jonathan M Davis jmdavisProg at gmx.com
Sat Jun 11 13:49:15 PDT 2011


On 2011-06-11 11:30, Ben Grabham wrote:
> 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.

Well, if you're using foreach, I wouldn't expect you to be calling popFront 
and front explicitly. So, I don't know what you're doing there. And you don't 
really use indices much with ranges, so I'm not sure what you're trying to do 
with them here. A forward range won't be consumed by a foreach (an input range 
would, I believe, since  it has no way to save it, but most ranges are at 
least forward ranges). Rather, a copy of it will be consumed. So, iterating 
over a range, won't consume it, if that's what you're trying to avoid. And if 
you're iterating over a range explicitly with popFront, then you can save the 
range with its save property prior to calling popFront. If it's just select 
values from a range that you're trying to save, then you're likely going to 
need to either stick them in a new container or save their indices (presumably 
by using a counter when iterating over the range) so that you can know where 
in the range they are to grab them up later (though that's not terribly 
efficient if the range isn't random access).

And if that information doesn't help you, I really don't know what to say, 
because I don't understand what you're trying to do based on what you've said. 
What you've said makes me wonder if there's something key about ranges that 
you don't understand or whether there's just miscommunication going on here. 
So, if you need better advice, you're probably going to have to show me the 
code.

- Jonathan M Davis


More information about the Digitalmars-d mailing list