It is the year 2020: why should I use / learn D?
Steven Schveighoffer
schveiguy at gmail.com
Wed Nov 14 17:33:27 UTC 2018
On 11/14/18 11:09 AM, Eugene Wissner wrote:
> On Wednesday, 14 November 2018 at 15:49:48 UTC, jmh530 wrote:
>> On Wednesday, 14 November 2018 at 15:33:49 UTC, rikki cattermole wrote:
>>> [snip]
>>>
>>> Really butchered. From what I can see they never mentioned D in any
>>> of the documents (kinda glad tbh). Those documents even question what
>>> it should be doing...
>>>
>>
>> I recall D being briefly mentioned in the Range specification. They
>> rejected D's approach because they wanted to build on existing
>> iterator-based code.
>
> 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."
>
> C++ iterators are more flexible. I think of things like rotate and
> bringToFront - in C++ you need begin, end and middle iterators. In D you
> can't have something like a "middle", you have to pass two independent
> ranges. But since most algorithms require begin and end iterators I like
> D ranges because they aren't that verbose. But D ranges aren't always
> nicer, for example SList.insertAfter requires a hack with accepting
> Take!Range instead of just Range.
The solution is cursors -- a "range" of a single element, which points
at that element. You can iterate it just like a range (it has front,
popFront, and empty), but it's used basically to mark the location you
are interested in.
dcollections used cursors, which I found much nicer. For example, you
can run a search on a tree, and get a reference to a single node,
instead of a range. You can then compose ranges using cursors for any
desired subsection:
auto allElementsBefore = tree[tree.begin .. tree.find("someValue")];
auto allElementsAfter = tree[tree.find("someValue") .. $];
auto elementsBetween = tree[tree.find("value1") .. tree.find("value2")];
insert just takes a cursor, everything is good.
-Steve
More information about the Digitalmars-d
mailing list