Why is std.algorithm so complicated to use?

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Tue Jul 10 08:30:30 PDT 2012


On 7/10/12 9:56 AM, Jacob Carlborg wrote:
> On 2012-07-10 15:28, Andrei Alexandrescu wrote:
>
>> We can arrange things in the library that a custom message is issued, or
>> in the compiler to do it once for all. At any rate whenever there's an
>> error pointing somewhere in the library's code that's an insufficient
>> template constraint that should be fixed.
>
> I mean, is it possible to have the original code work?
>
> auto bar = foo.chain("bar");
>
> Or perhaps more appropriate:
>
> auto bar = foo.append("bar");

It would be possible to chain a single element to the end of a range. 
One related thing to do is to define singletonRange(x) that returns a 
range with exactly one element, namely x.

>> I understand. So you need to use array() to convert the lazy map result
>> into an eager array. I disagree this is unintuitive, if it were then
>> very little of D would make sense are lazy, non-array ranges are
>> everywhere.
>
> Tell me what is the point of std.algorithm and ranges if I have to
> convert every single result of an algorithm to an array before I can use
> it with an other algorithm? I thought the whole idea was to avoid
> allocations between different usages of algorithms.

Ranges and std.algorithm obey simple, mathematical realities deriving 
from algorithm and storage topology constraints. For example sort works 
in place so it generally can't work on mapped stuff because there's no 
place to put the sorted contents. Also sort needs a random-access range 
to work with so it can't work e.g. with the result of filter, which 
necessarily yields a non-random-access range. And so on.

Now I understand if you come from a place where there's no concern over 
hidden allocations and extra work for the benefit of convenience, you 
may find std.algorithm less easy to work with. But drawing the 
conclusion that std.algorithm is badly designed or gratuitously 
difficult to use would be a mistake. I opine I can recognize a good vs. 
bad design even when it's mine, and in my opinion std.algorithm is a 
good design and that most of your opposing impressions derive from a 
misunderstanding of its charter.


Andrei




More information about the Digitalmars-d mailing list