Why is std.algorithm so complicated to use?

Christophe Travert travert at phare.normalesup.org
Tue Jul 10 09:01:06 PDT 2012

Andrei Alexandrescu , dans le message (digitalmars.D:171717), a écrit :
> On 7/10/12 11:11 AM, Christophe Travert wrote:
>> If you do not want the heap allocation of the array, you can create a
>> one-element range to feed to chain (maybe such a thing could be placed
>> in phobos, next to takeOne).
>> struct OneElementRange(E)
>> {
>>    E elem;
>>    bool passed;
>>    @property ref E front() { return elem; }
>>    void popFront() { passed = true; }
>>    @property bool empty() { return passed; }
>>    @property size_t length() { return 1-passed; }
>>    //...
>> }
> Yah, probably we should add something like this:
> auto singletonRange(E)(E value)
> {
>      return repeat(value).takeExactly(1);
> }

It would be much better to use:

auto singletonRange(E)(E value)
     return repeat(value).takeOne;

as well as:

auto emptyRange(E)(E value)
  return repeat(value).takeNone;

to have the advantages of takeOne and takeNone over takeExactly.

> I don't think it would be considerably less efficient than a handwritten 
> specialization. But then I've been wrong before in assessing efficiency.

Error message displaying the type of singletonRange(E) will be weird, 
but that's far from being the first place where it will be. Simplicity 
and maintainance of phobos seems more important to me. At least until 
these algorithm get stable, meaning open bug reports on algorithm and 
range are solved, and new bugs appears rarely. Optimisers should have no 
trouble inlining calls to Repeat's methods...

More information about the Digitalmars-d mailing list