Creating a Sub-view of a non - RA (hasSlicing) range.

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sat Jun 30 07:22:05 PDT 2012


On 6/30/12 8:24 AM, monarch_dodra wrote:
> On Saturday, 30 June 2012 at 11:35:07 UTC, Tobias Pankrath wrote:
>> std.algorithm.take.
>>
>> But I would generally avoid SList.
>
> Thanks, but isn't that kind of a crutch solution?
>
> I mean:
>
> 1) It creates a new type, so any attempt to use it to modify an existing
> range becomes impossible:
> void main()
> {
> SList!int slist;
> foreach(i; iota(5,0,-1))
> slist.insertFront(i);
>
> sr1 = take(sr1, 2); //Nope, not gonna happen
> writeln(sr1);
> }

One can't expect that to happen more than suspending a law of physics. 
The state needed for iterating an entire slist is a pointer. The state 
needed for iterating at most n elements of an slist is a pointer plus 
and integer. They're just different notions.

> 2) The new range is defined as a fixed length from the beginning of the
> range, as opposed to start and finish points. If I were to insert new
> items into my Slist, the new range would just bump the top items out of
> its range.

SList's range is not defined by start and finish points. It's defined as 
the start point, and the finish point is implicit by use of a sentinel 
(the null pointer).

> 3) This doesn't work for BidirectionalRanges: The resulting range is
> forward only. For the same reason, it is not possible to have a TakeLast
> for bidirectional ranges.

Not sure I understand this, but when we get into the realm of bidir 
ranges, things get a fair amount better. How would TakeLast work?


Andrei


More information about the Digitalmars-d mailing list