[Python-ideas] Re: Add builtin function for min(max())

2020-07-11 Thread Random832
On Thu, Jul 9, 2020, at 15:32, Dominik Vilsmeier wrote: > On 09.07.20 21:04, Ethan Furman wrote: > > I'm having a hard time understanding this line: > > > >    if lower == upper is not None: > > > > As near as I can tell, `upper is not None` will be either True or > > False, meaning the

[Python-ideas] Re: Add builtin function for min(max())

2020-07-09 Thread Greg Ewing
On 10/07/20 8:05 am, Christopher Barker wrote: if not (withrdrawal_amount > balance):     give_cash_to_customer(withdrawal_amount) Unfortunately, with my luck they will have coded it as if withdrawal_amount <= balance: give_cash_to_customer(withdrawal_amount) But my bigger concern is

[Python-ideas] Re: Add builtin function for min(max())

2020-07-09 Thread Ethan Furman
Operator chaining -- gotcha. Thanks Dominik and Ronald! -- ~Ethan~ ___ 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/

[Python-ideas] Re: Add builtin function for min(max())

2020-07-09 Thread Christopher Barker
Hmm, if my bank account balance is NaN, then: if not (withrdrawal_amount > balance): give_cash_to_customer(withdrawal_amount) would be pretty nice :-) -CHB On Thu, Jul 9, 2020 at 7:47 AM Eric Fahlgren wrote: > Yeah, but "not a number of pounds" sure beats seeing "£inf". > > On Thu, Jul

[Python-ideas] Re: Add builtin function for min(max())

2020-07-09 Thread Dominik Vilsmeier
On 09.07.20 21:04, Ethan Furman wrote: On 07/03/2020 05:03 PM, Steven D'Aprano wrote: def clamp(value, lower, upper): """Clamp value to the closed interval lower...upper. The limits lower and upper can be set to None to mean -∞ and +∞ respectively.

[Python-ideas] Re: Add builtin function for min(max())

2020-07-09 Thread Ronald Oussoren via Python-ideas
> On 9 Jul 2020, at 21:04, Ethan Furman wrote: > > On 07/03/2020 05:03 PM, Steven D'Aprano wrote: > >> def clamp(value, lower, upper): >> """Clamp value to the closed interval lower...upper. >> The limits lower and upper can be set to None to >> mean -∞ and +∞

[Python-ideas] Re: Add builtin function for min(max())

2020-07-09 Thread Ethan Furman
On 07/03/2020 05:03 PM, Steven D'Aprano wrote: def clamp(value, lower, upper): """Clamp value to the closed interval lower...upper. The limits lower and upper can be set to None to mean -∞ and +∞ respectively. """ if not (lower is None or upper

[Python-ideas] Re: Add builtin function for min(max())

2020-07-09 Thread Eric Fahlgren
Yeah, but "not a number of pounds" sure beats seeing "£inf". On Thu, Jul 9, 2020 at 3:25 AM Jonathan Fine wrote: > Off topic: I saw this NaN article this morning. > > Title: Hungry? Please enjoy this delicious NaN, courtesy of British Gas > and Sainsbury's > URL:

[Python-ideas] Re: Add builtin function for min(max())

2020-07-09 Thread Greg Ewing
On 9/07/20 10:25 pm, Jonathan Fine wrote: I particularly liked the Tideford Butterscotch Rice Pudding, at NaNp per 100g. I'd be a bit worried that if I bought one of those, I'd end up with a balance of NaN in my bank account. I hate to think what sort of havoc that would cause... -- Greg

[Python-ideas] Re: Add builtin function for min(max())

2020-07-09 Thread Jonathan Fine
Off topic: I saw this NaN article this morning. Title: Hungry? Please enjoy this delicious NaN, courtesy of British Gas and Sainsbury's URL: https://www.theregister.com/2020/07/09/bork/ I particularly liked the Tideford Butterscotch Rice Pudding, at NaNp per 100g. -- Jonathan

[Python-ideas] Re: Add builtin function for min(max())

2020-07-07 Thread Rob Cliffe via Python-ideas
On 06/07/2020 10:44, Federico Salerno wrote: On 05/07/2020 23:55, Rob Cliffe wrote: I don't think the new function should be restricted to numbers.  There may be uses for strings, or for user-built classes; why restrict it unnecessarily? If it quacks like supporting __lt__ and __gt__, it's

[Python-ideas] Re: Add builtin function for min(max())

2020-07-06 Thread Steven D'Aprano
On Mon, Jul 06, 2020 at 10:46:03AM -0700, Christopher Barker wrote: > 1) The math module is written in C, and Guido at least (he was still BDFL > then) rejected the idea of refactoring it to allow Python components (yes, > that was proposed, and even started by, I think, Victor Stiner). Yes, you

[Python-ideas] Re: Add builtin function for min(max())

2020-07-06 Thread Rob Cliffe via Python-ideas
On 06/07/2020 01:59, Greg Ewing wrote: If one of the bounds is missing, you don't need clamp(), you can use min() or max(). Not true. It would be true if you knew a specific one of the bounds was always missing, but you might want to call it repeatedly, sometimes with either bound (or

[Python-ideas] Re: Add builtin function for min(max())

2020-07-06 Thread Christopher Barker
On Mon, Jul 6, 2020 at 11:28 AM Chris Angelico wrote: > On Tue, Jul 7, 2020 at 3:49 AM Christopher Barker > wrote: > > > > [*] Honestly, while you *could* use clamp() with any comparable objects, > I really don't see a use case for anything non-numerical, but why not? > > Depends how broadly

[Python-ideas] Re: Add builtin function for min(max())

2020-07-06 Thread Chris Angelico
On Tue, Jul 7, 2020 at 3:49 AM Christopher Barker wrote: > > [*] Honestly, while you *could* use clamp() with any comparable objects, I > really don't see a use case for anything non-numerical, but why not? > Depends how broadly you define "numerical". Is a datetime numerical? It's a reasonable

[Python-ideas] Re: Add builtin function for min(max())

2020-07-06 Thread Christopher Barker
While I agree that having "clamp" operate on all Python types is a "good thing"[*], there is a complication. If it were put in the math module, then it would pretty much have to be a floating point function. At least that was the case for math.isclose(). My first prototype of isclose() was

[Python-ideas] Re: Add builtin function for min(max())

2020-07-06 Thread Bruce Leban
On Mon, Jul 6, 2020 at 4:27 AM Richard Damon wrote: > > > Supported, yes. Recommended, no. IEEE-754 specifies that the *minimum > > *operation propagates NaNs while the /alternate /*minimumNumber > > *operation drops NaNs. > > It should be noted, this is a change in the Standard that is just 1

[Python-ideas] Re: Add builtin function for min(max())

2020-07-06 Thread Richard Damon
On 7/5/20 11:07 PM, Bruce Leban wrote: > > On Sun, Jul 5, 2020 at 5:49 PM Steven D'Aprano > wrote: > > On Sun, Jul 05, 2020 at 11:58:58AM -0700, Bruce Leban wrote: > > But using a NAN is not an unreasonable operation. > > > I didn't say that it was. A NaN is

[Python-ideas] Re: Add builtin function for min(max())

2020-07-06 Thread Federico Salerno
On 05/07/2020 23:55, Rob Cliffe wrote: I think None should be allowed as a missing bound. +1 I don't think the new function should be restricted to numbers.  There may be uses for strings, or for user-built classes; why restrict it unnecessarily? If it quacks like supporting __lt__ and

[Python-ideas] Re: Add builtin function for min(max())

2020-07-05 Thread David Mertz
On Sun, Jul 5, 2020, 10:25 PM Steven D'Aprano wrote: > > The standard library *does* seem to have taken pains to avoid > "finite nans." > > I don't know what you mean by "finite nans". By definition, any NAN is not > considered finite. > The scare quotes because I know that's not a real thing.

[Python-ideas] Re: Add builtin function for min(max())

2020-07-05 Thread Chris Angelico
On Mon, Jul 6, 2020 at 12:36 PM Steven D'Aprano wrote: > Four years ago, there was strong opposition to giving the bounds default > values. I think the consensus at the time was that it is okay to > explicitly provide "unbounded" arguments (whether you spell them as > infinities, NANs, or None)

[Python-ideas] Re: Add builtin function for min(max())

2020-07-05 Thread Steven D'Aprano
On Mon, Jul 06, 2020 at 03:21:04AM +0100, Henk-Jaap Wagenaar wrote: > I do not agree clamp should be restricted to numeric values. I would expect > clamp to be agnostic to the specifics of floats/numbers and like sort > expect it to work for any values as long as they compare (using a dunder). It

[Python-ideas] Re: Add builtin function for min(max())

2020-07-05 Thread Steven D'Aprano
On Sun, Jul 05, 2020 at 09:42:07PM -0400, David Mertz wrote: > The standard library *does* seem to have taken pains to avoid "finite > nans." I don't know what you mean by "finite nans". By definition, any NAN is not considered finite. py> math.isfinite(math.nan) False Do you mean,

[Python-ideas] Re: Add builtin function for min(max())

2020-07-05 Thread Henk-Jaap Wagenaar
I do not agree clamp should be restricted to numeric values. I would expect clamp to be agnostic to the specifics of floats/numbers and like sort expect it to work for any values as long as they compare (using a dunder). I think having something like min=-math.inf is hence right out in my mind. If

[Python-ideas] Re: Add builtin function for min(max())

2020-07-05 Thread David Mertz
On Sun, Jul 5, 2020 at 10:04 PM Christopher Barker wrote: > Since you brought that up -- I recall a lot of debate about whether NaN's > should be considered missing values or "poisoning" in the statistics module > -- there are arguments for both, and neither was clear or obvious. So using > NaN

[Python-ideas] Re: Add builtin function for min(max())

2020-07-05 Thread Christopher Barker
On Sun, Jul 5, 2020 at 6:51 PM David Mertz wrote: > > will always return x for every finite and infinite value, so it must >> return x for NANs too. >> > > I strongly agree with Steven here. Also about order-dependence in results > of min() and max() being disturbing and contrary to IEEE-754. >

[Python-ideas] Re: Add builtin function for min(max())

2020-07-05 Thread Christopher Barker
On Sun, Jul 5, 2020 at 6:01 PM Greg Ewing wrote: > On 6/07/20 3:55 am, Steven D'Aprano wrote: > > With that interpretation, a NAN passed as the lower or upper bounds can > > be seen as another way of saying "no lower bounds" (i.e. negative > > infinity) or "no upper bounds" (i.e. positive

[Python-ideas] Re: Add builtin function for min(max())

2020-07-05 Thread David Mertz
On Sun, Jul 5, 2020 at 9:38 PM Steven D'Aprano wrote: > > >I agree with you that `clamp(lower=x, value=NAN, upper= x)` should > > >return x. > > Sorry Greg, on this point at least the IEEE-754 standard is firm: if a > function will return the same result for every non-NAN argument, then it >

[Python-ideas] Re: Add builtin function for min(max())

2020-07-05 Thread David Mertz
On Sun, Jul 5, 2020 at 8:57 PM Steven D'Aprano wrote: > On Sun, Jul 05, 2020 at 12:15:27PM -0400, David Mertz wrote: > > This is a digression, but does anyone have a nice example IN PYTHON of > > arriving at a NaN without going through infinity. I think Julia is right > > and Python is wrong

[Python-ideas] Re: Add builtin function for min(max())

2020-07-05 Thread Steven D'Aprano
On Mon, Jul 06, 2020 at 12:59:28PM +1200, Greg Ewing wrote: > >I agree with you that `clamp(lower=x, value=NAN, upper= x)` should > >return x. > > I don't think I agree with that Sorry Greg, on this point at least the IEEE-754 standard is firm: if a function will return the same result for

[Python-ideas] Re: Add builtin function for min(max())

2020-07-05 Thread Greg Ewing
On 6/07/20 3:55 am, Steven D'Aprano wrote: With that interpretation, a NAN passed as the lower or upper bounds can be seen as another way of saying "no lower bounds" (i.e. negative infinity) or "no upper bounds" (i.e. positive infinity), not "some unknown bounds". Python already has a value

[Python-ideas] Re: Add builtin function for min(max())

2020-07-05 Thread Steven D'Aprano
On Sun, Jul 05, 2020 at 12:15:27PM -0400, David Mertz wrote: > This is a digression, but does anyone have a nice example IN PYTHON of > arriving at a NaN without going through infinity. I think Julia is right > and Python is wrong about '0/0', but as things are, that's not an example. I wouldn't

[Python-ideas] Re: Add builtin function for min(max())

2020-07-05 Thread Steven D'Aprano
On Sun, Jul 05, 2020 at 11:58:58AM -0700, Bruce Leban wrote: > > People who want NAN poisoning can opt-in by doing a check for NANs > > themselves, either in a wrapper function, or by testing the bounds > > *once* ahead of time and then just calling the stdlib `clamp` once they > > know they

[Python-ideas] Re: Add builtin function for min(max())

2020-07-05 Thread Christopher Barker
Sorry, that got sent too soon. On Sun, Jul 5, 2020 at 1:59 PM Christopher Barker wrote: > On Sun, Jul 5, 2020 at 6:15 AM MRAB wrote: > >> > clamp(value: Number, minimum: Union[Number, Iterable[Number]], maximum: >> Union[Number, Iterable[Number]]) >> > > > What would that return? What if the

[Python-ideas] Re: Add builtin function for min(max())

2020-07-05 Thread MRAB
On 2020-07-05 14:06, MRAB wrote: On 2020-07-05 10:09, Federico Salerno wrote: [snip] FInally, I'd like to lit another fire: given that `min()` and `max()` accept an arbitrary amount of arguments, and that the signature that seems to be preferred for the hypothetical clamp at the moment sees

[Python-ideas] Re: Add builtin function for min(max())

2020-07-05 Thread Rob Cliffe via Python-ideas
On 05/07/2020 10:09, Federico Salerno wrote: On the topic of NaNs: I think in the face of ambiguity, we should refuse the temptation to guess and raise ValueError when the values given are not comparable to the point of clearly determining whether `value` is or isn't within `minimum` and

[Python-ideas] Re: Add builtin function for min(max())

2020-07-05 Thread David Mertz
On Sun, Jul 5, 2020, 3:51 PM Chris Angelico wrote: > On Mon, Jul 6, 2020 at 2:15 AM David Mertz wrote: > > > > This is a digression, but does anyone have a nice example IN PYTHON of > arriving at a NaN without going through infinity. I think Julia is right > and Python is wrong about '0/0', but

[Python-ideas] Re: Add builtin function for min(max())

2020-07-05 Thread Christopher Barker
On Sun, Jul 5, 2020 at 6:15 AM MRAB wrote: > > clamp(value: Number, minimum: Union[Number, Iterable[Number]], maximum: > Union[Number, Iterable[Number]]) > > Ss ( IsA Zzz What would that return? What if the iterable were two different lengths? If anything, I would accept an iterable for

[Python-ideas] Re: Add builtin function for min(max())

2020-07-05 Thread Chris Angelico
On Mon, Jul 6, 2020 at 2:15 AM David Mertz wrote: > > This is a digression, but does anyone have a nice example IN PYTHON of > arriving at a NaN without going through infinity. I think Julia is right and > Python is wrong about '0/0', but as things are, that's not an example. > Not sure why

[Python-ideas] Re: Add builtin function for min(max())

2020-07-05 Thread Bruce Leban
On Sun, Jul 5, 2020 at 8:57 AM Steven D'Aprano wrote: > > In the absense of any clear winner, my position is that NAN poisoning > should be opt-in. We should pick the option which inconveniences people > who want the other the least . > > Let's say the stdlib uses Option 1. The function doesn't

[Python-ideas] Re: Add builtin function for min(max())

2020-07-05 Thread Federico Salerno
On 05/07/2020 16:15, Steven D'Aprano wrote: Perhaps you should reconsider your expectations there. They do different things because they are different functions with different signatures and different purposes. It isn't even necessary to use min and max in the implementation of clamp, in fact it

[Python-ideas] Re: Add builtin function for min(max())

2020-07-05 Thread David Mertz
This is a digression, but does anyone have a nice example IN PYTHON of arriving at a NaN without going through infinity. I think Julia is right and Python is wrong about '0/0', but as things are, that's not an example. On Sun, Jul 5, 2020, 12:05 PM Chris Angelico wrote: > On Mon, Jul 6, 2020 at

[Python-ideas] Re: Add builtin function for min(max())

2020-07-05 Thread Chris Angelico
On Mon, Jul 6, 2020 at 1:58 AM Steven D'Aprano wrote: > Python makes it quite hard to get a NAN from the builtins, but other > languuages do not. Here's Julia: > > julia> 0/0 > NaN > > So there's at least one NAN which means *there is no correct answer*. >>> 1e1000-1e1000 nan ChrisA

[Python-ideas] Re: Add builtin function for min(max())

2020-07-05 Thread MRAB
On 2020-07-05 15:15, Steven D'Aprano wrote: [snip] The only differences I can see are that my implementation of clamp() supports None as a short-hand for infinity; and that it treats NANs according to the standard, unlike the builtin min and max, which manage to provide the worst of both

[Python-ideas] Re: Add builtin function for min(max())

2020-07-05 Thread Steven D'Aprano
On Sat, Jul 04, 2020 at 09:11:34AM -0700, Ben Rudiak-Gould wrote: Quoting William Kahan, one of the people who designed IEEE-754 and NANs: > What he says there (on page 9) is > > >Some familiar functions have yet to be defined for NaN . For instance > >max{x, y} should deliver the same result

[Python-ideas] Re: Add builtin function for min(max())

2020-07-05 Thread Stephen J. Turnbull
2qdxy4rzwzuui...@potatochowder.com writes: > On 2020-07-05 at 12:18:54 +0900, > "Stephen J. Turnbull" wrote: > > > Which suggests the question: Is there a commonly used equivalent for > > complex numbers? > > How would that work? Complex numbers are unordered, but I suspect that > you

[Python-ideas] Re: Add builtin function for min(max())

2020-07-05 Thread Steven D'Aprano
On Sat, Jul 04, 2020 at 01:01:15AM +0100, MRAB wrote: > Should it raise an exception if minimum > maximum? I think there are only two reasonable answers to this: - raise an exception if the lower bounds is greater than the upper bounds ("errors should never pass silently"); - or Do What I

[Python-ideas] Re: Add builtin function for min(max())

2020-07-05 Thread Steven D'Aprano
On Sat, Jul 04, 2020 at 10:16:45AM +0200, Federico Salerno wrote: > Yes, I'd expect ValueError if min > max or max < min. Is there a difference between those two conditions? *wink* > On 04/07/2020 02:03, Steven D'Aprano wrote: > >Bottom line is that passing a NAN as the lower or upper bound

[Python-ideas] Re: Add builtin function for min(max())

2020-07-05 Thread MRAB
On 2020-07-05 10:09, Federico Salerno wrote: [snip] FInally, I'd like to lit another fire: given that `min()` and `max()` accept an arbitrary amount of arguments, and that the signature that seems to be preferred for the hypothetical clamp at the moment sees `value` as the first positional

[Python-ideas] Re: Add builtin function for min(max())

2020-07-05 Thread Alex Hall
On Sun, Jul 5, 2020 at 2:36 PM Federico Salerno wrote: > On the topic of NaNs: I think in the face of ambiguity, we should refuse > the temptation to guess and raise ValueError when the values given are not > comparable to the point of clearly determining whether `value` is or isn't > within

[Python-ideas] Re: Add builtin function for min(max())

2020-07-05 Thread Federico Salerno
On the topic of NaNs: I think in the face of ambiguity, we should refuse the temptation to guess and raise ValueError when the values given are not comparable to the point of clearly determining whether `value` is or isn't within `minimum` and `maximum` or which of the two bounds it exceeds.

[Python-ideas] Re: Add builtin function for min(max())

2020-07-05 Thread Jeff Allen
On 05/07/2020 05:39, Steven D'Aprano wrote: On Sun, Jul 05, 2020 at 12:18:54PM +0900, Stephen J. Turnbull wrote: > and I'd like to toss a possible `coerce` Here my issue is that for me the *target* of a coercion should be a "single thing", which could be a type, but might also be a scalar.

[Python-ideas] Re: Add builtin function for min(max())

2020-07-05 Thread Steven D'Aprano
On Sun, Jul 05, 2020 at 06:35:38PM +1200, Greg Ewing wrote: > All in all, I think "clamp" is the best term for this. +1 -- Steven ___ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org

[Python-ideas] Re: Add builtin function for min(max())

2020-07-05 Thread Greg Ewing
On 5/07/20 4:39 pm, Steven D'Aprano wrote: Complex numbers represent points on a plane; it is very common in graphical toolkits to need to clamp an object to within some window or other region of the plane, But graphical toolkits don't treat points as complex numbers. The question is whether

[Python-ideas] Re: Add builtin function for min(max())

2020-07-04 Thread Steven D'Aprano
On Sun, Jul 05, 2020 at 12:18:54PM +0900, Stephen J. Turnbull wrote: > The problem with making this a builtin is that I don't think that > "clamp" is an unlikely identifier. Doesn't matter -- we can shadow builtins, it's just a name, not a keyword. Lots of code in the wild uses str, len, id,

[Python-ideas] Re: Add builtin function for min(max())

2020-07-04 Thread 2QdxY4RzWzUUiLuE
On 2020-07-05 at 12:18:54 +0900, "Stephen J. Turnbull" wrote: > Which suggests the question: Is there a commonly used equivalent for > complex numbers? How would that work? Complex numbers are unordered, but I suspect that you know that. Dan ___

[Python-ideas] Re: Add builtin function for min(max())

2020-07-04 Thread Stephen J. Turnbull
Federico Salerno writes: > I feel clip fits best with the idea of a collection to... clip. True, but you can (for this purpose) think of a scalar as a singleton. Still, I think "clamp" is by far the best of the bunch (though I don't see a need for this function in the stdlib, and definitely not

[Python-ideas] Re: Add builtin function for min(max())

2020-07-04 Thread Random832
On Sat, Jul 4, 2020, at 15:57, 2qdxy4rzwzuui...@potatochowder.com wrote: > > Simplifying the signature, in Python we have: > > > > def min(*iterable): > > iterator = iter(iterable) > > minimum = next(iterable) > > for item in iterator: > > if item < minimum: > >

[Python-ideas] Re: Add builtin function for min(max())

2020-07-04 Thread Steven D'Aprano
On Sat, Jul 04, 2020 at 11:54:47AM -0700, Christopher Barker wrote: > Hmm. > > > Since NaN is neither greater than nor less that anything, it seems the only > correct answer to any > Min,max,clamp involving a NaN is NaN. If you want NAN-poisoning behaviour it is easy for you to add it yourself

[Python-ideas] Re: Add builtin function for min(max())

2020-07-04 Thread Greg Ewing
On 5/07/20 7:33 am, Henk-Jaap Wagenaar wrote: min(0, float('nan')) == 0 and same for max. I would hence expect clamp to behave similarly. But min(float('nan'), 0) == nan. I don't think you can conclude anything from either of these about how clamp() *should* behave in the presence of nans,

[Python-ideas] Re: Add builtin function for min(max())

2020-07-04 Thread 2QdxY4RzWzUUiLuE
On 2020-07-04 at 20:33:36 +0100, Regarding "[Python-ideas] Re: Add builtin function for min(max())," Henk-Jaap Wagenaar wrote: > On Sat, 4 Jul 2020 at 19:58, Christopher Barker wrote: > > > Hmm. > > > > > > Since NaN is neither greater than nor less that

[Python-ideas] Re: Add builtin function for min(max())

2020-07-04 Thread Henk-Jaap Wagenaar
On Sat, 4 Jul 2020 at 19:58, Christopher Barker wrote: > Hmm. > > > Since NaN is neither greater than nor less that anything, it seems the > only correct answer to any > Min,max,clamp involving a NaN is NaN. > > Simplifying the signature, in Python we have: def min(*iterable): iterator =

[Python-ideas] Re: Add builtin function for min(max())

2020-07-04 Thread Christopher Barker
Hmm. Since NaN is neither greater than nor less that anything, it seems the only correct answer to any Min,max,clamp involving a NaN is NaN. -CHB On Sat, Jul 4, 2020 at 9:15 AM Ben Rudiak-Gould wrote: > On Fri, Jul 3, 2020 at 5:07 PM Steven D'Aprano > wrote: > > As I recall, there was some

[Python-ideas] Re: Add builtin function for min(max())

2020-07-04 Thread Ben Rudiak-Gould
On Fri, Jul 3, 2020 at 5:07 PM Steven D'Aprano wrote: > As I recall, there was some positive support but it ran out of steam > because nobody could agree on how to handle NANs even though the > IEEE-754 standard tells us how to handle them *wink* > > See my responses at the time re NANs here: > >

[Python-ideas] Re: Add builtin function for min(max())

2020-07-04 Thread Piper Thunstrom
On Sat, Jul 4, 2020 at 8:29 AM Federico Salerno wrote: > On 04/07/2020 02:50, Christopher Barker wrote: > > FWIW, numpy calls it "clip" > > I feel clip fits best with the idea of a collection to... clip. `clamp()` > would work with scalars, for which the word clip might not be clear at a >

[Python-ideas] Re: Add builtin function for min(max())

2020-07-04 Thread Dan Sommers
On Saturday, July 4, 2020, at 03:16 -0500, Federico Salerno wrote: On 04/07/2020 02:50, Christopher Barker wrote: FWIW, numpy calls it "clip" I feel clip fits best with the idea of a collection to... clip. `clamp()` would work with scalars, for which the word clip might not be clear at

[Python-ideas] Re: Add builtin function for min(max())

2020-07-04 Thread Federico Salerno
On 04/07/2020 02:01, MRAB wrote: Should it raise an exception if minimum > maximum? If it doesn't, then you'll get a different answer depending on whether it's `min(max(value, minimum), maximum)` or `max(min(value, maximum), minimum)`. Yes, I'd expect ValueError if min > max or max < min.

[Python-ideas] Re: Add builtin function for min(max())

2020-07-03 Thread Steven D'Aprano
On Fri, Jul 03, 2020 at 09:24:47PM -0300, Soni L. wrote: > how do you plan to clamp a numpy array or a string? I'm not saying it is meaningful, but it certainly works to clamp strings: py> s = "hello" py> min(max(s, "a"), "z") 'hello' Likewise other indexable types can be compared

[Python-ideas] Re: Add builtin function for min(max())

2020-07-03 Thread Soni L.
On 2020-07-03 9:37 p.m., Christopher Barker wrote: On Fri, Jul 3, 2020 at 5:25 PM > wrote: > I'd go for val[min:max] tbh. another reason this is Not Good: in slicing syntax, a:b means >=a and < b -- this asymmetry is not what we would want here. It doesn't

[Python-ideas] Re: Add builtin function for min(max())

2020-07-03 Thread Christopher Barker
FWIW, numpy calls it "clip": numpy.clip(a, a_min, a_max, out=None, **kwargs) Clip (limit) the values in an array. Given an interval, values outside the interval are clipped to the interval edges. For example, if an interval of [0, 1] is specified, values smaller than 0 become 0, and

[Python-ideas] Re: Add builtin function for min(max())

2020-07-03 Thread Christopher Barker
On Fri, Jul 3, 2020 at 5:25 PM wrote: > > I'd go for val[min:max] tbh. > another reason this is Not Good: in slicing syntax, a:b means >=a and < b -- this asymmetry is not what we would want here. -CHB -- Christopher Barker, PhD Python Language Consulting - Teaching - Scientific

[Python-ideas] Re: Add builtin function for min(max())

2020-07-03 Thread Matthew Einhorn
On Fri, Jul 3, 2020 at 5:57 PM Soni L. wrote: > > > On 2020-07-03 6:05 p.m., Federico Salerno 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

[Python-ideas] Re: Add builtin function for min(max())

2020-07-03 Thread Soni L.
On 2020-07-03 7:53 p.m., tcphon...@gmail.com wrote: > I'd go for val[min:max] tbh. > benefits: > > - currently not allowed! > - replaces min and max! Is this a serious suggestion? No offence intended, but this seems ill-thought-out. val[min:max] is perfectly legal syntax and it will only

[Python-ideas] Re: Add builtin function for min(max())

2020-07-03 Thread TCPhone93
> I'd go for val[min:max] tbh. > benefits: > > - currently not allowed! > - replaces min and max! Is this a serious suggestion? No offence intended, but this seems ill-thought-out. val[min:max] is perfectly legal syntax and it will only error if the variable val happens to not support

[Python-ideas] Re: Add builtin function for min(max())

2020-07-03 Thread Steven D'Aprano
This was proposed about four years ago, Here is a link to the first post in the thread: https://mail.python.org/pipermail/python-ideas/2016-July/041262.html Discussion spilled over into the following month, here's the first post following:

[Python-ideas] Re: Add builtin function for min(max())

2020-07-03 Thread MRAB
On 2020-07-03 22:05, Federico Salerno 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`

[Python-ideas] Re: Add builtin function for min(max())

2020-07-03 Thread MRAB
On 2020-07-03 22:56, Soni L. wrote: On 2020-07-03 6:05 p.m., Federico Salerno 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

[Python-ideas] Re: Add builtin function for min(max())

2020-07-03 Thread Soni L.
On 2020-07-03 6:05 p.m., Federico Salerno 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

[Python-ideas] Re: Add builtin function for min(max())

2020-07-03 Thread Jonathan Goble
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