<div dir="ltr"><div class="gmail_default" style="font-family:monospace,monospace">I looked hard at #min:max: some years ago and decided it<br></div><div class="gmail_default" style="font-family:monospace,monospace">was way too confusing to ever use. My own library has<br><br></div><div class="gmail_default" style="font-family:monospace,monospace">Magnitude>><br> median: lower and: upper<br> "I considered adding a method<br><br> clampedBetween: lower and: upper<br> ^self < lower ifTrue: [lower] ifFalse: [<br> upper < self ifTrue: [upper] ifFalse: [self]]<br><br> This sends #< once or twice. The only snag is that it<br> doesn't make much sense unless lower <= upper, and it<br> doesn't check that.<br><br> Squeak has a method<br><br> min: min max: max<br> ^(self min: min) max: max<br><br> which could be simplified to<br><br> min: min max: max<br> |t|<br> t := min < self ifTrue: [min] ifFalse: [self].<br> ^max < t ifTrue: [max] ifFalse: [t]<br><br> The name here is a little confusing. min: x max: y is<br> pretty much the same as clampedBetween: y and: x.<br> This version always does two comparisons, but still makes<br> little or no sense unless min >= max, which it does not<br> check. It would be easy enough for me to add the Squeak<br> method, but I find it far too confusing to use.<br><br> I wanted a method which gave the same result as<br> clampedBetween:and: whenever that made sense, and which<br> took no more than two comparisons whenever clampedBetween:and:<br> or min:max: would have made sense, but which always makes<br> sense as long as all the arguments are comparable.<br><br> The answer is simple: the median. Actually, it isn't quite<br> the answer. If the receiver lies between lower and upper, it<br> takes two comparisons. If not, it takes a third comparison<br> to decide which of the two end-points to return, which seems<br> fair enough, because if we don't _assume_ the relative order<br> of the end-points, we have to _find out_."<br><br> ^self < lower<br> ifTrue: [ "self < lower"<br> self < upper<br> ifFalse: [ "upper <= self < lower" self]<br> ifTrue: [ "self < lower, self < upper"<br> upper < lower<br> ifTrue: ["self < upper < lower" upper]<br> ifFalse: ["self < lower <= upper" lower]]]<br> ifFalse: [ "lower <= self"<br> upper < self<br> ifFalse: [ "lower <= self <= upper" self]<br> ifTrue: [ "lower <= self, upper <= self"<br> upper < lower<br> ifTrue: ["upper < lower <= self" lower]<br> ifFalse: ["lower <= upper <= self" upper]]]<br><br></div><div class="gmail_default" style="font-family:monospace,monospace">That is, (x median: y and: z) returns the middle value,<br></div><div class="gmail_default" style="font-family:monospace,monospace">whatever order x y and z are in.<br><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On 21 April 2018 at 23:41, Ben Coman <span dir="ltr"><<a href="mailto:btc@openinworld.com" target="_blank">btc@openinworld.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div class="h5"><br><div class="gmail_extra"><br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class="m_-2477269966546508634gmail-HOEnZb"><div class="m_-2477269966546508634gmail-h5">On Sat, 21 Apr 2018, Ben Coman wrote:<br>
<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On 21 April 2018 at 03:51, Hilaire <<a href="mailto:hilaire@drgeo.eu" target="_blank">hilaire@drgeo.eu</a>> wrote:<br>
Hi,<br>
<br>
Out of curiosity.<br>
<br>
I always found the #min:max: confusing and lost in its expressiveness.<br>
<br>
One should write:<br>
<br>
10 min: 48 max: 12<br>
<br>
to expect 12.<br>
<br>
but logically one (at least me) may want to express it as:<br>
<br>
10 min: 12 max: 48<br>
<br>
Then when reading its source code, it is even more confusing:<br>
<br>
min: aMin max: aMax<br>
^ (self min: aMin) max: aMax<br>
<br>
Are not the argument names inversed in their meaning, if any?<br>
<br>
<br>
I would agree. I see most use by Color like...<br>
<br>
Color>>adjustBrightness: brightness<br>
"Adjust the relative brightness of this color. (lowest value is 0.005 so that hue information is not lost)"<br>
<br>
^ self class<br>
h: self hue<br>
s: self saturation<br>
v: (self brightness + brightness min: 1.0 max: 0.005)<br>
alpha: self alpha<br>
<br>
Trying to read that twists my brain. <br>
<br>
<br>
I can understand the intent from the implementation <br>
min: aMin max: aMax <br>
^ (self min: aMin) max: aMax<br>
<br>
but that message might more properly be #min:thenMax:<br>
However something like <br>
(self brightness + brightness boundedBy: 0.005 and: 1.0)<br>
(self brightness + brightness boundedMin: 0.005 m<wbr>ax: 1.0)<br>
seems more intention revealing.<br>
<br>
<br>
Altering #min:max semantics would make awful portability,<br>
but perhaps it should forward to a new method to make it clear the other is preferred.<br>
<br>
Would the Squeak community be amenable to a similar change?<br>
I'd be happy to contribute the changes to the Squeak Inbox.<br></blockquote></div></div></blockquote></div><br></div></div></div><div class="gmail_extra">
<span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">On 21 April 2018 at 17:56, Levente Uzonyi<span> </span></span><span dir="ltr" style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial"><<a href="mailto:leves@caesar.elte.hu" style="color:rgb(17,85,204)" target="_blank">leves@caesar.elte.hu</a>></span><span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline"><span> </span><wbr>wrote:</span><br style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial"><blockquote class="gmail_quote" style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Squeak has #clampLow:high: for this reason.<span class="m_-2477269966546508634gmail-HOEnZb"><font color="#888888"><br></font></span><div class="m_-2477269966546508634gmail-HOEnZb"><div class="m_-2477269966546508634gmail-h5" style="display:block;color:rgb(80,0,80)"><br></div></div></blockquote>
<br></div><div class="gmail_extra">
<span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">Thanks Levente. </span>Thats a good one.</div><div class="gmail_extra">With similar usage clamping signals in electronics, this is worthwhile to adopt.</div><div class="gmail_extra"><br></div><div class="gmail_extra"><a href="https://pharo.fogbugz.com/f/cases/21758/Replace-min-max-with-clampLow-High" target="_blank">https://pharo.fogbugz.com/f/<wbr>cases/21758/Replace-min-max-<wbr>with-clampLow-High</a><br></div><div class="gmail_extra"><br></div><div class="gmail_extra">cheers -ben</div><div class="gmail_extra"><br></div></div>
</blockquote></div><br></div>