On 03/24/2012 11:19 AM, Stewart Gordon wrote:
> The documentation for std.range states
>
> http://dlang.org/phobos/std_range.html
> "This module defines the notion of range (by the membership tests
> isInputRange, isForwardRange, isBidirectionalRange,
> isRandomAccessRange), range capability tests (such as hasLength or
> hasSlicing), and a few useful range incarnations."
>
> But that intro doesn't describe what a range actually is.
Apparently the library relies on the definition elsewhere. [1]
> Here's what I've made out so far:
>
> - Ranges are basically a generalised API for accessing a container or
> stream that is linear in logical structure and can have any element type.
Yes. Some ranges can also provide random access.
> - What is a range and what isn't is determined by compile-time duck
> typing - testing whether it implements certain methods.
Yes.
> - A range over a finite data structure is essentially equivalent to a
> start/end iterator pair as used by various C++ STL functions.
Yes.
> - Where a function in std.range is described as iterating through ranges
> in a particular way, what this function does is to return a range that
> delivers the resulting sequence.
Yes. Almost always lazily.
> Have I got all this right?
Yes.
> Things I'm confused by:
>
> - One of the expansions of put is
> r.front = e; r.popFront();
>
> What has putting something at the front of a range and then popping it
> to do with outputting to the range?
Iterating an output range is also by popFront(). So what it says is, put
this element to the output range and advance the range. There is a
gotcha about this when the output range is a slice: Whatever is just put
into the range is popped right away! :) [2]
> - Why is a range that can save checkpoints called a "forward" range?
I agree. Here is my guess: The name of the ForwardRange comes from the
fact that it is not double-ended. It can go only in one direction.
> Stewart.
Ali
[1] http://www.informit.com/articles/printerfriendly.aspx?p=1407357
[2] http://ddili.org/ders/d.en/ranges.html