So... add maxCount and maxPos?

Andrei Alexandrescu via Digitalmars-d digitalmars-d at puremagic.com
Wed Jan 20 17:11:19 PST 2016


On 01/20/2016 04:22 PM, Ivan Kazmenko wrote:
> On Wednesday, 20 January 2016 at 17:16:00 UTC, Andrei Alexandrescu wrote:
>> This is one of those cases in which we were pedantically right to not
>> add them, but their equivalents look like crap.
>>
>> https://github.com/D-Programming-Language/phobos/pull/3942
>
> As it seems related, let me share my personal experience with minPos.
> Most of the time, I want one of the two things related to an extremum.
>
> 1. The minimum or maximum element itself.  I write it as
> a.minPos.front.  That's almost fine, but maybe there is a more
> expressive way?

Sadly, no. I'm very willing to add an overload of min and max for one 
argument (which fortunately does not compile currently) to compute these 
over a range. In fact literally today I wrote r.min and was surprised it 
didn't work. Would you want to embark on that? Code, docs, unittests, 
the works.

> 2. The index of minimum or maximum element, mostly using plain array as
> a range.  I write "a.length - a.minPos.length", and it looks completely
> unintuitive.  Additionally, when compiling to 64 bits, I usually still
> want an int to use with other ints around (longs are overkill most of
> the time), and the cast adds more clobber.  Again, is there a better way
> to express that?

No better way currently. We could add (min|max)Index but I like 
(min|max)Pos quite a bit. They offer more flexibility for non-random 
access ranges. For random access ranges you should soon be able to write 
a.before(a.minPos).length, for a different kind of unintuitiveness :o).


Andrei


More information about the Digitalmars-d mailing list