<div dir="ltr"><div>I see a discussion of how the one-sided clamps can be implemented only for floating-point using NaN, but my concern is the API for Ord types generally. ‘clamp (lo, hi) x = min hi (max x lo)’ allows ‘atLeast lo = clamp (lo, 0/0)’ and ‘atMost hi = clamp (0/0, hi)’, which is excellent for providing reasonable NaN handling, but doesn’t say anything about e.g. integers. It does point to the fact that the correct implementations of ‘atLeast’ and ‘atMost’, for consistency with the correct ‘clamp’ for floats, are actually these:<br><br>atMost = min<br>atLeast = flip max -- rather than just ‘max’<br><br></div><div>To me, that’s another argument that these should be considered, since it’s the kind of subtle distinction that libraries should be handling for users, along the same lines as the stability of ‘min’ & ‘max’ (namely: ‘a == b ==> (min a b, max a b) == (a, b)’). Again, this doesn’t necessarily need to go in the same MR, but they are closely related.</div><div><br></div><div>If they were included, it would be necessary to include a note in the documentation that the correct order is ‘atMost hi . atLeast lo’ if someone is applying them separately, but that ‘clamp’ should be preferred for automatically doing this.<br><br></div><div>I don’t know offhand how this would be disrupted down the line if we changed the ‘Ord’ instance for floats to use the IEEE-754 total ordering, but that should also be considered for all these functions.<br></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Aug 27, 2020 at 9:05 AM Carter Schonwald <<a href="mailto:carter.schonwald@gmail.com">carter.schonwald@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">actually, if you look at the associated ticket, we have a version of clamp that gives the right way to derive the onesided behaviors for even floating point! (and has the correct / desirable behavior in the presence of NANs! )</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Aug 26, 2020 at 8:38 PM Jon Purdy <<a href="mailto:evincarofautumn@gmail.com" target="_blank">evincarofautumn@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>I’m also strongly for ‘clamp :: (Ord a) => (a, a) -> a -> a’.</div><div><br></div><div>Even if we don’t resolve it now, I do want to mention that, in discussing this with some acquaintances recently, we agreed that one-sided clamps likely warrant a home in ‘Data.Ord’ as well:<br></div><div><br>atLeast :: (Ord a) => a -> a -> a<br>atLeast = max<br></div><div>{-# INLINE atLeast #-}</div><div><br>atMost :: (Ord a) => a -> a -> a<br>atMost = min<br><div>{-# INLINE atMost #-}</div><br>clamp :: (Ord a) => (a, a) -> a -> a<br>clamp (lower, upper) = atLeast lower . atMost upper<br><br>While their implementations are identical to ‘max’ and ‘min’, semantically they privilege their arguments differently, serving as documentation of intent in code like ‘nonnegative = fmap (atLeast 0)’. The hope is that this may help reduce bugs caused by the common error of mixing up ‘min’ and ‘max’, owing to the unfortunate false friendship between “at least/most” and “the least/most”.<br></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, Aug 16, 2020 at 2:43 AM Henning Thielemann <<a href="mailto:lemming@henning-thielemann.de" target="_blank">lemming@henning-thielemann.de</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><br>
On Fri, 14 Aug 2020, Sandy Maguire wrote:<br>
<br>
> It seems to me that base is missing the very standard function `clamp :: Ord a => a -> a -> a -> a`:<br>
> <br>
> ```haskell<br>
> clamp :: Ord a => a -> a -> a -> a<br>
> clamp low high = min high .max low<br>
> ```<br>
<br>
<br>
<a href="https://hackage.haskell.org/package/utility-ht-0.0.15/docs/Data-Ord-HT.html#v:limit" rel="noreferrer" target="_blank">https://hackage.haskell.org/package/utility-ht-0.0.15/docs/Data-Ord-HT.html#v:limit</a><br>
_______________________________________________<br>
Libraries mailing list<br>
<a href="mailto:Libraries@haskell.org" target="_blank">Libraries@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries</a><br>
</blockquote></div>
_______________________________________________<br>
Libraries mailing list<br>
<a href="mailto:Libraries@haskell.org" target="_blank">Libraries@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries</a><br>
</blockquote></div>
</blockquote></div>