www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Using a range as a reference

reply "Paolo Invernizzi" <paolo.invernizzi gmail.com> writes:
Hi all,

I've a custom generator, and is modelled as a structure 
forwardRange, and I want to be able to use it like this, printing 
the first ten fibonacci:

---
struct ForEacher(Range){
     Range* r;
     this(ref Range r_){ r=&r_; }
      property bool empty(){ return r.empty; }
      property auto front(){ return r.front; }
     void popFront(){ r.popFront(); }
}
auto forEacher(Range)(ref Range r){
     return ForEacher!Range(r);
}
void main(){
     auto fib = recurrence!("a[n-1] + a[n-2]")(1, 1);

     foreach(e; fib.forEacher){
         static i = 0; i++; if(i == 5) break;
         writeln(e);
     }
     foreach(e; fib.forEacher){
         static j = 0; j++; if(j == 5) break;
         writeln(e);
     }

     or

     writeln(fib.forEacher.take(5));
     writeln(fib.forEacher.take(5));

}
---

There's something in phobos like the 'forEacher'? What is the 
best strategy for something like that?
Aug 24 2013
parent reply David <d dav1d.de> writes:
do you mean refRange?

Aug 24 2013
parent reply "Paolo Invernizzi" <paolo.invernizzi gmail.com> writes:
On Saturday, 24 August 2013 at 19:51:14 UTC, David wrote:
 do you mean refRange?

Exactly, thank you! I've missed it as it is not listed in the tables of the range documentation, like the others, but only in the top-index... - Paolo Invernizzi
Aug 24 2013
parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Sat, Aug 24, 2013 at 10:02:56PM +0200, Paolo Invernizzi wrote:
 On Saturday, 24 August 2013 at 19:51:14 UTC, David wrote:
do you mean refRange?

Exactly, thank you! I've missed it as it is not listed in the tables of the range documentation, like the others, but only in the top-index...
[...] That would be a documentation bug. Please file a bug on http://d.puremagic.com/issues. T -- Arise, you prisoners of Windows / Arise, you slaves of Redmond, Wash, / The day and hour soon are coming / When all the IT folks say "Gosh!" / It isn't from a clever lawsuit / That Windowsland will finally fall, / But thousands writing open source code / Like mice who nibble through a wall. -- The Linux-nationale by Greg Baker
Aug 24 2013
parent "Paolo Invernizzi" <paolo.invernizzi gmail.com> writes:
On Saturday, 24 August 2013 at 20:24:47 UTC, H. S. Teoh wrote:
 On Sat, Aug 24, 2013 at 10:02:56PM +0200, Paolo Invernizzi 
 wrote:
 
 I've missed it as it is not listed in the tables of the range
 documentation, like the others, but only in the top-index...
That would be a documentation bug. Please file a bug on http://d.puremagic.com/issues.
Done: http://d.puremagic.com/issues/show_bug.cgi?id=10885 - Paolo Invernizzi
Aug 24 2013