www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 10039] New: std.algorithm enhancements: min, max, clamp

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10039

           Summary: std.algorithm enhancements: min, max, clamp
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: diggsey googlemail.com



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))

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 07 2013
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10039


qznc web.de changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |qznc web.de



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)))

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 21 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10039




Pull request for clamp:
https://github.com/D-Programming-Language/phobos/pull/1360

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 21 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10039


bearophile_hugs eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs eml.cc



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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 21 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10039





 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. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jun 21 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10039






 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? -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jun 21 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10039


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |yebblies gmail.com





 
 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)); } -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Aug 02 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10039


monarchdodra gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |monarchdodra gmail.com




 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. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Aug 18 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10039






 TLDR: I think this allowing single arg min/max is a bad idea.
I agree. Let's close WONTFIX this issue? -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Aug 18 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=10039






 
 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 -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Aug 18 2013