D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 10039 - std.algorithm enhancements: min, max, clamp
Summary: std.algorithm enhancements: min, max, clamp
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P2 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-05-07 11:57 UTC by Diggory
Modified: 2020-03-21 03:56 UTC (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Diggory 2013-05-07 11:57:51 UTC
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 qznc 2013-06-21 06:50:29 UTC
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 qznc 2013-06-21 07:05:55 UTC
Pull request for clamp: https://github.com/D-Programming-Language/phobos/pull/1360
Comment 3 bearophile_hugs 2013-06-21 09:31:35 UTC
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 Diggory 2013-06-21 14:12:30 UTC
(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 bearophile_hugs 2013-06-21 14:50:15 UTC
(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 yebblies 2013-08-02 21:36:04 UTC
(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 monarchdodra 2013-08-18 03:24:05 UTC
(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 bearophile_hugs 2013-08-18 05:36:59 UTC
(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 monarchdodra 2013-08-18 05:50:57 UTC
(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 github-bugzilla 2014-02-14 13:27:09 UTC
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 12 basile-z 2015-11-21 12:36:09 UTC
min(), max() & clamp() are all in std.algorithm.comparison now.