Thread overview
[Issue 10039] New: std.algorithm enhancements: min, max, clamp
May 07, 2013
Diggory
Jun 21, 2013
qznc@web.de
Jun 21, 2013
qznc@web.de
Jun 21, 2013
Diggory
Aug 03, 2013
yebblies
May 07, 2013
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


--- Comment #0 from Diggory <diggsey@googlemail.com> 2013-05-07 11:57:51 PDT ---
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: -------
June 21, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10039


qznc@web.de changed:

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


--- Comment #1 from qznc@web.de 2013-06-21 06:50:29 PDT ---
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: -------
June 21, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10039



--- Comment #2 from qznc@web.de 2013-06-21 07:05:55 PDT ---
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: -------
June 21, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10039


bearophile_hugs@eml.cc changed:

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


--- Comment #3 from bearophile_hugs@eml.cc 2013-06-21 09:31:35 PDT ---
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: -------
June 21, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10039



--- Comment #4 from Diggory <diggsey@googlemail.com> 2013-06-21 14:12:30 PDT ---
(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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 21, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10039



--- Comment #5 from bearophile_hugs@eml.cc 2013-06-21 14:50:15 PDT ---
(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?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 03, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10039


yebblies <yebblies@gmail.com> changed:

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


--- Comment #6 from yebblies <yebblies@gmail.com> 2013-08-03 14:36:04 EST ---
(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));
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 18, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10039


monarchdodra@gmail.com changed:

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


--- Comment #7 from monarchdodra@gmail.com 2013-08-18 03:24:05 PDT ---
(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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 18, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10039



--- Comment #8 from bearophile_hugs@eml.cc 2013-08-18 05:36:59 PDT ---
(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?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 18, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10039



--- Comment #9 from monarchdodra@gmail.com 2013-08-18 05:50:57 PDT ---
(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

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------