couple of really noob questions (ranges, toString)

Jesse Phillips jessekphillips+D at gmail.com
Mon Nov 1 19:33:41 PDT 2010


spir Wrote:

> I'm certain its designer has a heavy Lisp baggage! I would rather have a hasNext() and next() duo.]

To build on what Jonathon wrote. There are three benefits that I have enjoyed having front and popFront instead of the next and hasNext.

The first is simply that if I want to make use of the element multiple times I don't need to find storage for it, I can just call front every time I need it.

The second is when building a range you don't need to look ahead to find out if another element is available. This means I don't have to store the next value along with the current value. For example std.algorithm has a function filter. You provide it with a Range and ask it to only return values that meet some criteria. Since the elements that will be returned aren't going to be the number in the original Range it can't just take its length.

And with that you don't end up with unneeded calculations if the next element isn't needed.

The one annoying thing with this has been the need to initialize the Range (calling popFront after creating the range). Since the first item may not be an item that meets the criteria, or that there are any items, the range needs to be advanced so that it finds the correct element.


More information about the Digitalmars-d-learn mailing list