I think a good way to learn ranges is by looking at the interface to them, 
compared to those used by other languages. For one thing, C++ iterators are way 
more complicated than what you will find in many other languages; for example 
Java's iterator interface[1] is similar to a D range[2].

In both languages the interface is 3 functions (there are many types of ranges 
which require more, but only one kind of iterator).

Java:
bool hasNext()
E next()
void remove()

D:
bool empty()
E front()
void popFront()

These look almost identical but the semantics are very different. For example, 
hasNext() requires a look-ahead, while empty() does not. This is important 
since you may not be able to look ahead in a range.

next() performs the equivalent of calling front(); popFront(); And remove() has 
nothing to do with the iterator as it performs on the underlining collection.

Removing the look-ahead is probably one of the biggest improvements over Java's 
iterator.

1. http://java.sun.com/j2se/1.5.0/docs/api/java/util/Iterator.html
2. http://digitalmars.com/d/2.0/phobos/std_range.html#isInputRange

clueless bystander Wrote:

> Watching D evolve from the outside there seems to be a lot of ongoing 
> discussion
> on this newsgroup about the D range idiom which is somehow opposed to 
> conventional
> thinking about iterators.
> 
> Can someone please explain in plain words just exactly what a range is and how
> it differs from the iterator concept (if that's appropriate???) and what are 
> the benefits
> from a data modeling and access perspective.
> 
> Sure, I'm clueless, though suspect many other bystanders would appreciate a
> succinct heads-up.
> 
> Thanks,
> clueless bystander

Reply via email to