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?
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.
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.
Orthogonality is in my opinion overrated. I, for one, do not want to
move through problem space in a Manhattan taxicab. The shortest path
between two points is a straight line. That is trivial fact.
I understand that other people may have a different stance on this, but
my personal experience is that not using most of the generic
functionality in std.algorithm produces way faster and better
self-documenting code. In functional style programs, good function names
are crucial if the code should be understandable fast, and saving a few
keystrokes matters a great deal because lines fill up fast.
Timon