My vote: clamp(+1000, min=-1, max=+1)

I've had to do this operation many times in a variety of languages (Python
is my playtoy for personal stuff, but work and school rarely give me a
choice of language). I always love it when the language has a readily
available `clamp()` function for this, and quietly grumble when I have to
build it myself (usually as two lines to avoid the readability issue of
nested function calls). It's one of those minor annoyances that you don't
realize how annoying it is until you work in a language that hands you a
simple `clamp` function for free (or at the cost of one import), and then
you wonder "why doesn't <my favorite language> have that?"

It's simple enough IMO to go straight into the stdlib, possibly even
builtins. The maintenance burden of such a simple function would be almost
zero, and I doubt there are a lot of third-party uses of the name `clamp`
that aren't implementing this operation as a utility function (and that
would also care about having access to the new builtin; after all, existing
uses would continue to work and just shadow the new builtin).

If we want to bikeshed now, I prefer the signature I used above for my
vote. More specifically: clamp(val, /, min, max). Alternatively, clamp(val,
min, max, /) is also fine by me.

On Fri, Jul 3, 2020 at 5:17 PM Federico Salerno <salerno...@gmail.com>
wrote:

> Even after years of coding it sometimes takes me a moment to correctly
> parse expressions like `min(max(value, minimum), maximum)`, especially when
> the parentheses enclose some local computation instead of only references,
> and the fact that `min` actually needs the *maximum* value as an argument
> (and vice-versa) certainly doesn't help.
>
> It's a little surprising to me how shorthand functions akin to CSS's
> `clamp()` aren't more popular among modern programming languages. Such a
> tool is, in my opinion, even more missed in Python, what with it being
> focussed on readability and all. There would likely also be some (probably
> minor) performance advantages with implementing it at a builtins level.
>
> Example usage:
>
> >>> val = 100
> >>> clamp(10, val, 50)
> 50
> >>> val = 3
> >>> clamp(10, val, 50)
> 10
> >>> val = 25
> >>> clamp(10, val, 50)
> 25
>
> I'm undecided whether I would like `clamp`, `minmax` or something else as
> a name. I'm curious of other ideas.
>
> As far as the signature is concerned, I like both `clamp(min, val, max)`
> for the logical position of the three arguments (which mirrors expressions
> such as `min < val < max`) and `clamp(val, min=x, max=y)`. I prefer the
> former, but declaring them as normal positional-and-keyword arguments would
> allow the programmer to use an alternative order if they so choose.
> _______________________________________________
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/KWAOQFSV3YJYQV2Y5JXGXFCXHJ3WFLRS/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/AIXRQTV2NL7ZBW3FRR6QAWSWDW5QAAQ6/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to