<div dir="ltr"><div class="gmail_default" style="font-family:&quot;comic sans ms&quot;,sans-serif;color:rgb(39,78,19)">Dave,</div><div class="gmail_default" style="font-family:&quot;comic sans ms&quot;,sans-serif;color:rgb(39,78,19)"><br></div><div class="gmail_default" style="font-family:&quot;comic sans ms&quot;,sans-serif;color:rgb(39,78,19)">Thanks for the feedback!</div><div class="gmail_default" style="font-family:&quot;comic sans ms&quot;,sans-serif;color:rgb(39,78,19)"><br></div><div class="gmail_default" style="font-family:&quot;comic sans ms&quot;,sans-serif;color:rgb(39,78,19)">Yes, there is no explicit need to make `clamp` a global function, and the extension you proposed would work just fine I think.</div><div class="gmail_default" style="font-family:&quot;comic sans ms&quot;,sans-serif;color:rgb(39,78,19)"><br></div><div class="gmail_default" style="font-family:&quot;comic sans ms&quot;,sans-serif;color:rgb(39,78,19)">`CountableClosedRange` already provides a `clamped(to:)` method but that does not suit the use case I was thinking of wanting to receive a single scalar return value rather than a range. </div><div class="gmail_default" style="font-family:&quot;comic sans ms&quot;,sans-serif;color:rgb(39,78,19)"><br></div><div class="gmail_default" style="font-family:&quot;comic sans ms&quot;,sans-serif;color:rgb(39,78,19)">Any chance of dumping `min` and `max` as global functions?</div><div class="gmail_default" style="font-family:&quot;comic sans ms&quot;,sans-serif;color:rgb(39,78,19)">There is already an extension on `Array` for `min` and `max` but that might carry the overhead of creating an array every time a `min` or `max` is performed, unless there was some compiler optimization magic that could come to the rescue.</div><div class="gmail_default" style="font-family:&quot;comic sans ms&quot;,sans-serif;color:rgb(39,78,19)"><br></div><div class="gmail_default" style="font-family:&quot;comic sans ms&quot;,sans-serif;color:rgb(39,78,19)">- Nick </div>







</div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Mar 10, 2017 at 10:44 AM, David Sweeris <span dir="ltr">&lt;<a href="mailto:davesweeris@mac.com" target="_blank">davesweeris@mac.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><span class=""><br><div><blockquote type="cite"><div>On Mar 9, 2017, at 5:37 PM, Nicholas Maccharoli via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div><br class="m_2448634529046356981Apple-interchange-newline"><div><div dir="ltr"><div class="gmail_default" style="font-family:&quot;comic sans ms&quot;,sans-serif;color:rgb(39,78,19)">Swift Evolution, </div><div class="gmail_default" style="font-family:&quot;comic sans ms&quot;,sans-serif;color:rgb(39,78,19)"><br></div><div class="gmail_default" style="font-family:&quot;comic sans ms&quot;,sans-serif;color:rgb(39,78,19)">Having a`clamp` function available in the standard library would complement `min` and `max` really well.</div><div class="gmail_default" style="font-family:&quot;comic sans ms&quot;,sans-serif;color:rgb(39,78,19)"><br></div><div class="gmail_default" style="font-family:&quot;comic sans ms&quot;,sans-serif;color:rgb(39,78,19)">I mentioned this before when the discussion for Swift 4 was still in stage 1, but now that stage 2 has started I thought it might be worth while to bring up again.</div><div class="gmail_default" style="font-family:&quot;comic sans ms&quot;,sans-serif;color:rgb(39,78,19)"><br></div><div class="gmail_default" style="font-family:&quot;comic sans ms&quot;,sans-serif;color:rgb(39,78,19)">Still needs some work, but here is the draft proposal I wrote previously</div><div class="gmail_default" style="font-family:&quot;comic sans ms&quot;,sans-serif;color:rgb(39,78,19)"><br></div><div class="gmail_default"><font color="#274e13" face="comic sans ms, sans-serif"><a href="https://github.com/Nirma/swift-evolution/commit/a51c543a76e9a1021996fb4b617311a588e7f397" target="_blank">https://github.com/Nirma/<wbr>swift-evolution/commit/<wbr>a51c543a76e9a1021996fb4b617311<wbr>a588e7f397</a></font><br></div><div class="gmail_default"><br></div><div class="gmail_default"><font color="#274e13" face="comic sans ms, sans-serif">Basically it boils down to something like this.</font></div><div class="gmail_default"><font color="#274e13" face="comic sans ms, sans-serif"><br></font></div><div class="gmail_default"><pre style="box-sizing:border-box;font-family:sfmono-regular,consolas,&quot;liberation mono&quot;,menlo,courier,monospace;font-size:13.6px;margin-top:0px;margin-bottom:0px;font-stretch:normal;line-height:1.45;word-wrap:normal;padding:16px;overflow:auto;background-color:rgb(246,248,250);border-radius:3px;word-break:normal;color:rgb(36,41,46)"><span class="m_2448634529046356981gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">public</span> <span class="m_2448634529046356981gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">func</span> <span class="m_2448634529046356981gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">clamp</span>&lt;<span class="m_2448634529046356981gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">T</span> : <span class="m_2448634529046356981gmail-pl-e" style="box-sizing:border-box;color:rgb(121,93,163)"><span class="m_2448634529046356981gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Comparable</span></span>&gt;(<span class="m_2448634529046356981gmail-pl-smi" style="box-sizing:border-box;color:rgb(51,51,51)"><span class="m_2448634529046356981gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">value</span></span>: T, <span class="m_2448634529046356981gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">_</span> <span class="m_2448634529046356981gmail-pl-smi" style="box-sizing:border-box;color:rgb(51,51,51)">lower</span>: T, <span class="m_2448634529046356981gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">_</span> <span class="m_2448634529046356981gmail-pl-smi" style="box-sizing:border-box;color:rgb(51,51,51)">upper</span>: T) <span class="m_2448634529046356981gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">-&gt;</span> T {
  <span class="m_2448634529046356981gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">return</span> <span class="m_2448634529046356981gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">max</span>(lower, <span class="m_2448634529046356981gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">min</span>(value, upper))
}</pre></div><div class="gmail_default"><br></div><div class="gmail_default"><br></div><div class="gmail_default"><font color="#274e13" face="comic sans ms, sans-serif">What does the community think?</font></div></div></div></blockquote><br></div></span><div>IIRC, we’ve been avoiding top-level functions lately… What about as an extension on Range?</div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><div style="margin:0px;line-height:normal;font-family:Menlo;color:rgb(186,45,162)"><span style="font-variant-ligatures:no-common-ligatures">extension</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">Range</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000"> {</span></div></div><div><div style="margin:0px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">func</span><span style="font-variant-ligatures:no-common-ligatures"> clamp(</span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">_</span><span style="font-variant-ligatures:no-common-ligatures"> x: </span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187">Bound</span><span style="font-variant-ligatures:no-common-ligatures">) -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187">Bound</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></div></div><div><div style="margin:0px;line-height:normal;font-family:Menlo;color:rgb(112,61,170)"><span style="font-variant-ligatures:no-common-ligatures;color:#000000">        </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">return</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#3e1e81">max</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">(</span><span style="font-variant-ligatures:no-common-ligatures">lowerBound</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">, </span><span style="font-variant-ligatures:no-common-ligatures;color:#3e1e81">min</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">(x, </span><span style="font-variant-ligatures:no-common-ligatures">upperBound</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">))</span></div></div><div><div style="margin:0px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    }</span></div></div><div><div style="margin:0px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">}</span></div></div></blockquote><br><div>- Dave Sweeris</div></div></blockquote></div><br></div>