Iterators and Ranges: Comparing C++ to D to Rust
Dukc
ajieskola at gmail.com
Wed Jun 16 11:16:41 UTC 2021
On Tuesday, 15 June 2021 at 18:35:04 UTC, Walter Bright wrote:
> On 6/14/2021 7:29 AM, Steven Schveighoffer wrote:
>> I wonder if there is room for hobbled iterators (cursors) to
>> be in D in some capacity.
>
> Have them be an index, rather than a pointer.
This is a good generic solution for random access ranges. But
iterators are about any forward range.
For a forward range, I think something like this does it albeit
verbosely:
```
auto midAndTail = haystack.find(/*...*/);
auto head = haystack.recurrence!((r,n)=>r[n-1].dropOne).until!"a
is b"(midAndTail).map!"a.front";
```
But there is currently no way for a bidirectional range to
iterate forward and then turn back. We could agree on a generic
range primitive, say `turned`, that returns a range that iterates
back to start that is optional. But then the bidirectional range
that supports it will always be bigger because it needs to
remember it's start. Hard question.
More information about the Digitalmars-d
mailing list