Bug 10039 – std.algorithm enhancements: min, max, clamp

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-05-07T11:57:51Z
Last change time
2020-03-21T03:56:39Z
Assigned to
No Owner
Creator
Diggory

Comments

Comment #0 by diggsey — 2013-05-07T11:57:51Z
Since min/max are supposed to be variadic they should also accept a single parameter. min(var) == var max(var) == var Also a natural extension of min/max is clamp: clamp(a, b, var) == max(a, min(b, var))
Comment #1 by qznc — 2013-06-21T06:50:29Z
How does that unary min/max variant come up? With regard to clamp, there is also the question if it can/should be variadic? clamp(a,b,c,d) =?= max(a,min(b,max(c,d)))
Comment #2 by qznc — 2013-06-21T07:05:55Z
Comment #3 by bearophile_hugs — 2013-06-21T09:31:35Z
Regarding max(x) and min(x) what are the use cases? I don't see any. Regarding clamp(), sometimes I need a function like that. But I think it's better to design it like this: clamp(var, a, b) so it's usable in UFCS chains.
Comment #4 by diggsey — 2013-06-21T14:12:30Z
(In reply to comment #3) > Regarding max(x) and min(x) what are the use cases? I don't see any. > > Regarding clamp(), sometimes I need a function like that. But I think it's > better to design it like this: clamp(var, a, b) so it's usable in UFCS chains. The single parameter cases come up when using min/max in generic code. Otherwise it is necessary to special case it on every use. With regards to clamp I have no strong feelings about parameter order, etc.
Comment #5 by bearophile_hugs — 2013-06-21T14:50:15Z
(In reply to comment #4) > The single parameter cases come up when using min/max in generic code. > Otherwise it is necessary to special case it on every use. Can you show one real example of such generic code where this happens?
Comment #6 by yebblies — 2013-08-02T21:36:04Z
(In reply to comment #5) > (In reply to comment #4) > > > The single parameter cases come up when using min/max in generic code. > > Otherwise it is necessary to special case it on every use. > > Can you show one real example of such generic code where this happens? import std.algorithm; import std.stdio; int minpos(T...)(T args) { auto minv = min(args); foreach(i, v; args) if (v == minv) return i; return -1; } void main() { writeln(minpos(3, 2, 1)); writeln(minpos(2, 1)); writeln(minpos(1)); }
Comment #7 by monarchdodra — 2013-08-18T03:24:05Z
(In reply to comment #0) > Since min/max are supposed to be variadic they should also accept a single > parameter. > > min(var) == var > max(var) == var I did a pull to allow this, but I bailed out. While most of the time, I agree with such improvements, in this particular case, the names "min" and "max" are problematic, due to clashes with the built in .min and .max properties. In particular, it means you can now write: 5.max(); //This resolves to "5" which is different from: 5.max; //This resolves to "int.max" I think that making it so that (accidentally) adding parenthesis to "5.min" actually compiles, yet has a different meaning, is too dangerous for what it buys us. TLDR: I think this allowing single arg min/max is a bad idea.
Comment #8 by bearophile_hugs — 2013-08-18T05:36:59Z
(In reply to comment #7) > TLDR: I think this allowing single arg min/max is a bad idea. I agree. Let's close WONTFIX this issue?
Comment #9 by monarchdodra — 2013-08-18T05:50:57Z
(In reply to comment #8) > (In reply to comment #7) > > > TLDR: I think this allowing single arg min/max is a bad idea. > > I agree. Let's close WONTFIX this issue? Unfortunately, this is a "dual entry" entry. Let's just say that "min"/"max" is being closed as "WONTFIX" by this present comment. The issue is still open though, as "campl" is a valid request, and there is already an open pull for it referencing this entry: https://github.com/D-Programming-Language/phobos/pull/1360
Comment #10 by github-bugzilla — 2014-02-14T13:27:09Z
Commit pushed to master at https://github.com/D-Programming-Language/phobos https://github.com/D-Programming-Language/phobos/commit/4aab80ef5358b519df9153d5a1cfbd34d35643db Merge pull request #1461 from monarchdodra/issue10039 Fix Issue 10039 - std.algorithm enhancements: min, max, clamp
Comment #11 by github-bugzilla — 2014-07-17T17:25:35Z
Comment #12 by b2.temp — 2015-11-21T12:36:09Z
min(), max() & clamp() are all in std.algorithm.comparison now.