<div dir="ltr"><div class="gmail_default" style="font-family:"comic sans ms",sans-serif;color:rgb(39,78,19)">Swift Evolution Community,</div><div class="gmail_default" style="font-family:"comic sans ms",sans-serif;color:rgb(39,78,19)"><br></div><div class="gmail_default" style="font-family:"comic sans ms",sans-serif;color:rgb(39,78,19)">In the standard library there is a `min` and a `max` function but there isn't </div><div class="gmail_default" style="font-family:"comic sans ms",sans-serif;color:rgb(39,78,19)">a `bounds` function, or a function that takes a value and an upper and lower bound and returns that value if it is within the specified range or if not the closer of the lower or upper values supplied to the `bounds` function.</div><div class="gmail_default" style="font-family:"comic sans ms",sans-serif;color:rgb(39,78,19)">(Other possible names for such a function could be something like `clamp`, or `clip`)</div><div class="gmail_default" style="font-family:"comic sans ms",sans-serif;color:rgb(39,78,19)"><br></div><div class="gmail_default" style="font-family:"comic sans ms",sans-serif;color:rgb(39,78,19)">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:</div><div class="gmail_default" style="font-family:"comic sans ms",sans-serif;color:rgb(39,78,19)"><br></div><div class="gmail_default" style="font-family:"comic sans ms",sans-serif;color:rgb(39,78,19)">
<p class=""><span class=""> public</span><span class=""> </span><span class="">func</span><span class=""> bounds<T : </span><span class="">Comparable</span><span class="">>(value: </span><span class="">T</span><span class="">, </span><span class="">_</span><span class=""> lower: </span><span class="">T</span><span class="">, </span><span class="">_</span><span class=""> upper: </span><span class="">T</span><span class="">) -> </span><span class="">T</span><span class=""> {</span></p>
<p class=""><span class=""> </span><span class="">return</span><span class=""> </span><span class="">max</span><span class="">(lower, </span><span class="">min</span><span class="">(value, upper))</span></p>
<p class=""><span class=""> }</span></p><p class="">Does this sound like something the community thinks would be worthwhile to add?</p><p class="">If so I would love to write a quick proposal.</p><p class=""><br></p><p class="">- Nick </p><p class=""><br></p></div></div>