d unpopular why is

Vijay Nayar madric at gmail.com
Tue Jun 21 06:59:34 UTC 2022


On Monday, 20 June 2022 at 14:04:25 UTC, monkyyy wrote:
> tldr: std.containers is bad.

If I understand your point correct, it is that std.container 
classes appear to be almost empty, and yet there's a ton of 
algorithms in std.range and std.algorithm, and you find this to 
be imbalanced because all these algorithms are not useful without 
a data structure to act upon.

The way D is written with templates and ranges is certainly 
different than what you might expect from C++'s STL or Java's 
libraries, which has pros and cons.

The cons:
  - If you approach things by starting with the data structure 
(the noun), and then look for things you can do with it (verbs), 
then the code/docs can seem disorganized and scattered.
  - Taking time to understand templates/ranges takes time.
  - Each class which returns or takes in a range introduces a 
layer of templates which your code may have to account for.

The pros:
  - If you approach things by starting with the functions (verbs), 
then they can be applied to any data structure (noun) that you 
want, even new ones that you create.
  - A bit of time invested up front in understanding 
[templates/ranges](http://ddili.org/ders/d.en/ranges.html) 
applies to all data structures, not just one at a time.
  - The use of 
[std.range.interfaces](https://dlang.org/phobos/std_range_interfaces.html) can make writing your own code less complex when dealing with ranges.

Thankfully the number of methods that define what a range is is 
fairly small, thus creating your own struct to represent one for 
your own classes starts to become natural after a bit of practice.

There will be many cases where the logic you are trying to do, 
such as returning the minimum or maximum of a range requires that 
the entire range be consumed (e.g. by calling std.array : array). 
That's OK, just because you use ranges, it doesn't mean they're 
the cure-all for all problems. From an efficiency standpoint, 
this is one of the reasons that ForwardRanges are there, you can 
"save()" your position and have one function compute the max of 
the remaining elements, and then still iterate forward with other 
algorithms.

Even so, there are a few cases where I struggle to use ranges, 
especially cases where an algorithm "seeks" a particular value, 
and then starts iterating forwards and backwards to find 
particular data. Those cases often require a total rewrite of the 
algorithm.


More information about the Digitalmars-d mailing list