D Recurrences

Jonathan M Davis jmdavisProg at gmx.com
Thu Jun 9 12:15:30 PDT 2011


On 2011-06-09 11:59, Ben Grabham wrote:
> On 09/06/11 19:54, Ben Grabham wrote:
> > On 09/06/11 19:45, Andrej Mitrovic wrote:
> >> I never understood why it's called "open" interval. What does it 'open'?
> > 
> > Don't know the answer to that one, just know it's always been called a
> > open interval when both endpoints aren't included. D's foreach loops are
> > half-closed intervals.
> > 
> > Oh well, that's a shame.
> > 
> > I ended up writing a lazy cached list and filter implementation anyway :)
> > 
> > It may use more memory but it's worth it for me!
> > 
> > Thanks for all the help!
> > Nebster
> 
> Oh, I do have one question though,
> 
> How does the save property work? I can't seem to be able to return an
> integer (index) rather than the whole object. How can I do it?

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


More information about the Digitalmars-d mailing list