Slides from LASER 2012

Timon Gehr timon.gehr at gmx.ch
Thu Sep 20 16:27:43 PDT 2012


On 09/21/2012 12:51 AM, bearophile wrote:
> Timon Gehr:
>
>> chain has type Result. dynRange takes an arbitrary range and transforms
>> it into a range with the same value/vs reference behaviour whose static
>> type depends only on the element type.
>
> I see. So that chain() is the normal chain of Phobos :-)
>

Exactly.

> (But is DynRange a lazy stream/sequence? This is the most important
> thing, because creating an eager linked list is kind of easy already,
> and misses the main point of my "request".)
>
> Bye,
> bearophile

Proof of concept:

import std.range, std.algorithm;
struct DynRange(T){
     @property T front(){ return frontImpl(); }
     @property bool empty(){ return emptyImpl(); }
     void popFront(){
         auto u = popFrontImpl();
         frontImpl = u.frontImpl;
         emptyImpl = u.emptyImpl;
         popFrontImpl = u.popFrontImpl;
     }
private:
     T delegate() frontImpl;
     bool delegate() emptyImpl;
     DynRange!T delegate() popFrontImpl;
}
DynRange!(ElementType!R) dynRange(R)(R range)if(isInputRange!R){
     DynRange!(ElementType!R) result;
     result.frontImpl = ()=>range.front;
     result.emptyImpl = ()=>range.empty;
     result.popFrontImpl = (){
         auto newRange = range;
         newRange.popFront();
         return dynRange(newRange);
     };
     return result;
}

void main(){
     auto r = iota(0,10).dynRange;
     auto t = [1,2,3,4,5].dynRange;
     import std.stdio;
     writeln(r,r,t,t);
}

To allow the definition of recursive lazy ranges, we'd also need
a facility to 'delay' computation. I'll post a proof of concept
tomorrow, by implementing eg. a lazy prime sieve.




More information about the Digitalmars-d-announce mailing list