Using a range as a reference
Paolo Invernizzi
paolo.invernizzi at gmail.com
Sat Aug 24 12:31:38 PDT 2013
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?
More information about the Digitalmars-d-learn
mailing list