<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""></div><blockquote type="cite" class=""><div class="">On 25 Aug 2016, at 12:05, Nicholas Maccharoli wrote:<br class=""><br class="">I personally see merit in adding a function to bound the value of a variable within a range and think it would be simple to write with the existing implementations of `min` and `max` with something like:<br class=""><br class=""> public func bounds<T : Comparable>(value: T, _ lower: T, _ upper: T) -> T {<br class=""> return max(lower, min(value, upper))<br class=""> }<br class=""><br class="">Does this sound like something the community thinks would be worthwhile to add?<br class=""></div></blockquote><div class=""><br class=""></div><div class="">I'd welcome that addition. In terms of function interface, I think we can do better than the 3-argument `<span style="font-family: Menlo; font-size: 11px;" class="">clamp(x, min, max)</span>` function that is seen in several math libraries.</div><div class=""><br class=""></div><div class="">Our ***Range types already have a `<span style="font-family: Menlo; font-size: 11px;" class="">clamped(to:)</span>` member function, e.g. <a href="https://developer.apple.com/reference/swift/closedrange/1779071-clamped" class="">here's one for ClosedRange</a>. It creates a new range constraining the receiver's bounds within the new bounds given as argument.</div><div class=""><br class=""></div><div class="">I think the sensible thing would be to add a similar, and equally named, method to the Comparable protocol, taking in the <span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);" class="">ClosedRange</span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;" class=""><</span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);" class="">Self</span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;" class="">></span> to limit the value to:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class=""> extension</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">Comparable</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">public func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> clamped(to limits: </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">ClosedRange</span><span style="font-variant-ligatures: no-common-ligatures" class=""><</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">Self</span><span style="font-variant-ligatures: no-common-ligatures" class="">>) -> </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">Self</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class=""><</span><span style="font-variant-ligatures: no-common-ligatures" class=""> limits.</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">lowerBound</span><span style="font-variant-ligatures: no-common-ligatures" class=""> ? limits.</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">lowerBound</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> : </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">></span><span style="font-variant-ligatures: no-common-ligatures" class=""> limits.</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">upperBound</span><span style="font-variant-ligatures: no-common-ligatures" class=""> ? limits.</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">upperBound</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> : </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> (</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">-0.1</span><span style="font-variant-ligatures: no-common-ligatures" class="">).</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">clamped</span><span style="font-variant-ligatures: no-common-ligatures" class="">(to: </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span><span style="font-variant-ligatures: no-common-ligatures" class=""> ... </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span><span style="font-variant-ligatures: no-common-ligatures" class="">) </span><span style="font-variant-ligatures: no-common-ligatures; color: #008400" class="">// 0.0</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class=""> 3.14</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">clamped</span><span style="font-variant-ligatures: no-common-ligatures" class="">(to: </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span><span style="font-variant-ligatures: no-common-ligatures" class=""> ... .</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">infinity</span><span style="font-variant-ligatures: no-common-ligatures" class="">) </span><span style="font-variant-ligatures: no-common-ligatures; color: #008400" class="">// 3.14</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class=""> "foo"</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">clamped</span><span style="font-variant-ligatures: no-common-ligatures" class="">(to: </span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"a"</span><span style="font-variant-ligatures: no-common-ligatures" class=""> ... </span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"f"</span><span style="font-variant-ligatures: no-common-ligatures" class="">) </span><span style="font-variant-ligatures: no-common-ligatures; color: #008400" class="">// "f"</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class=""> "foo"</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">clamped</span><span style="font-variant-ligatures: no-common-ligatures" class="">(to: </span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"a"</span><span style="font-variant-ligatures: no-common-ligatures" class=""> ... </span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"g"</span><span style="font-variant-ligatures: no-common-ligatures" class="">) </span><span style="font-variant-ligatures: no-common-ligatures; color: #008400" class="">// "foo"</span></div></div><div class=""><br class=""></div><div class="">From my experience, I'd say it'd be most useful for clamping floating-point numbers and collection indices.</div><div class=""><br class=""></div><div class="">— Pyry</div><div class=""><br class=""></div></body></html>