Slides from LASER 2012

Timon Gehr timon.gehr at gmx.ch
Fri Sep 21 15:57:38 PDT 2012


On 09/21/2012 07:46 PM, Dmitry Olshansky wrote:
> On 21-Sep-12 03:27, Timon Gehr wrote:
>> 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.
>
> I swear I've seen it somewhere in Phobos:
> http://dlang.org/phobos/std_range.html#InputRangeObject
> and friends. Maybe we ought to lay a better infrastructure for it.
>

(Assuming you relate to the range part, and not the delay part.)
I am aware of that. That one is unusable in its current state.

import std.range;
void main(){
     auto r = iota(0,10).inputRangeObject;
     auto t = [1,2,3,4,5].inputRangeObject;
     import std.stdio;
     writeln(r,r,t,t); // [0,1,2,3,4,5,6,7,8,9][][1,2,3,4,5][]
}

Yes, I could use 'save' everywhere, but I really do not want to. The
'dynamic' range should behave the same as the one exposing the full
static type. It shouldn't be made so easy to make aliasing-related
errors.




More information about the Digitalmars-d-announce mailing list