On 9/15/11 3:36 PM, Timon Gehr wrote:
On 09/15/2011 05:42 PM, Andrei Alexandrescu wrote:
We now have:
min(a, b, c, ...)
max(a, b, c, ...)
minCount!callable(range)
minPos!callable(range)
The latter two take a binary predicate that customizes the comparison
function. That's why maxCount and maxPos are not provided.
Are you sure that
minCount!"a>b"(range);
is better than
maxCount(range); ?
If so, for what definition of better? Do you expect every library user
to implement the maxCount in terms of minCount himself in his own
utility module?
Don't forget that I'm as fallible as the next guy, if not more.
std.algorithm has some 8500 lines and I wrote most of them, so I was
liable to make a few imperfect choices.
I had the suspicion minCount and minPos where poor choices of name,
but I was unable to find a better name for "min" there. It's something
like
"extremum" or "bound". Finding a good name would be great.
I am quite sure that those names are a quite perfect match, because they
describe exactly what those functions do. Both extremum and bound are
too general terms, because what the functions do relates to the least
element (min) under the total order given by pred.
Reading the above I fail to understand whether you like these names or not.
After many years, mathematicians that think about it every day could not
find a satisfying solution either, eg, consider:
http://en.wikipedia.org/wiki/Least_element . They'll usually prove a
theorem for one of min or max and then argue that the proof works
analogously for the other one. If they want to refer specifically to min
or max, they use eg. the terms 'min' or 'max', as opposed to eg 'min'
and 'min in respect to the reverse of the total order used at other
places in this paper'.
I get that you think the names might be a poor choice because those
functions are supposed to be usable to get the maxCount or the maxPos
too. That is, in my opinion, the wrong conclusion. The problem is that
those functions are supposed to be usable to get the maxCount or the
maxPos in the first place. Functions called 'maxCount' and 'maxPos' are
way better suited, and providing these would not increase the complexity
of std.algorithm. It would only make it more regular.
Once minCount takes a lambda, I can't tell people what lambda to pass,
so they will be able to compute a maximum with it.
Defining maxCount and maxPos without a lambda is definitely possible,
but rather odd. How is one e.g. supposed to remember that minPos is the
one with a lambda and maxPos isn't?
I'd much rather look for a good name to replace both, and then define
minXxx and maxXxx in terms of that.
Andrei