It is the year 2020: why should I use / learn D?

Neia Neutuladh neia at ikeran.org
Wed Nov 14 17:59:07 UTC 2018


On Wed, 14 Nov 2018 16:09:32 +0000, Eugene Wissner wrote:
> No, it wasn't the reason. Some algorithms cannot be implemented with
> ranges as efficient as with iterators.
> 
> "In other words, by converting the is_word_boundary from iterators to
> D-style ranges, the algorithm goes from O(1) to O(N). From this we draw
> the following conclusion: D’s choice of algorithmic basis operations is
> inherently less efficient than C++’s."

The code in question:

```
bool is_word_boundary(Range1, Range2)( Range1 front, Range2 back )
    if (isBidirectionalRange!Range1 && isInputRange!Range2 )
{
    bool is_word_prev = front.empty ? false : isword(front.back);
    bool is_word_this = back.empty ? false : isword(back.front);
    return is_word_prev != is_word_this;
}

range r = myrange;
size_t n = 0;
for(range r = myrange; !r.empty; r.popFront(), ++n)
     if( is_word_boundary( takeExactly(myrange, n), r) )
        break;
```

This is fine for a random access range (takeExactly yields a random access 
range from it). But with a bidirectional range like a doubly linked list, 
takeExactly gives you a forward range; you can't efficiently construct 
`.back()` for it.

If we had a notion of splittable ranges, that would work. (All 
deterministic ranges are splittable, but unless the range provides a 
special implementation or is a random access range, it's O(N) to split.)

If subranges were a dedicated concept, that would also work; you could use 
a subrange to iterate through the outer range.

And as Steven Schveighoffer mentioned, generalizing indexes allows a lot of 
range types to be close enough to random access to make this use case 
efficient. I'm pretty sure this should be equal to C++ iterators.


More information about the Digitalmars-d mailing list