www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Pointers and Ranges

reply Xinok <xinok live.com> writes:
I'm new to ranges and while I understand the basic concept, I don't know 
everything about them. With arrays, you can simply use arr.ptr to infer 
a type automatically. So I was wondering, is there an equivalent for 
ranges? What I'm looking for is the ability to do *p as well as p[1] or 
p[-1] with ranges.
Oct 08 2011
parent Ali =?iso-8859-1?q?=C7ehreli?= <acehreli yahoo.com> writes:
On Sat, 08 Oct 2011 12:31:20 -0400, Xinok wrote:

 I'm new to ranges and while I understand the basic concept, I don't know
 everything about them. With arrays, you can simply use arr.ptr to infer
 a type automatically.
Or with ElementType. If Range is a template type: writefln("I have a range of %ss", (ElementType!Range).stringof); // for example, "I have a range of ints"
 So I was wondering, is there an equivalent for
 ranges? What I'm looking for is the ability to do *p
Depending on the range it can be - myRange.front - myRange.back - myRange[i]
 as well as p[1] or
Only with RandomAccessRanges.
 p[-1] with ranges.
That should be considered out of bounds with ranges, but it is possible to achieve with opIndex() for RandomAccessRanges. Ali P.S. I am in the process of translating my Turkish D book to English. Since it targets the beginner programmer and starts from the very basics, it may be a little boring for some. But I had decided to skip some chapters and start translating more interesting ones that normally come later in the book. The two chapters on Ranges happen to be the ones that I am currently working on. I may make the current state of the translation available in a few days. Here are the originals: http://ddili.org/ders/d/araliklar.html http://ddili.org/ders/d/araliklar_baska.html There is a Google translate bar on the left hand side with very limited results. (Humans are still better than computers! :)) Here is a little ForwardRange example: import std.stdio; import std.range; struct FibonacciSeries { int first = 0; int second = 1; static enum empty = false; // Alternatively: static immutable bool empty = false; property int front() const { return first; } void popFront() { int third = first + second; first = second; second = third; } FibonacciSeries save() const { return this; } } void main() { writeln(take(FibonacciSeries(), 10)); }
Oct 08 2011