<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=""><br class=""><div><blockquote type="cite" class=""><div class="">On Mar 9, 2017, at 5:37 PM, Nicholas Maccharoli via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="gmail_default" style="font-family:"comic sans ms",sans-serif;color:rgb(39,78,19)">Swift Evolution, </div><div class="gmail_default" style="font-family:"comic sans ms",sans-serif;color:rgb(39,78,19)"><br class=""></div><div class="gmail_default" style="font-family:"comic sans ms",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:"comic sans ms",sans-serif;color:rgb(39,78,19)"><br class=""></div><div class="gmail_default" style="font-family:"comic sans ms",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:"comic sans ms",sans-serif;color:rgb(39,78,19)"><br class=""></div><div class="gmail_default" style="font-family:"comic sans ms",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:"comic sans ms",sans-serif;color:rgb(39,78,19)"><br class=""></div><div class="gmail_default"><font color="#274e13" face="comic sans ms, sans-serif" class=""><a href="https://github.com/Nirma/swift-evolution/commit/a51c543a76e9a1021996fb4b617311a588e7f397" class="">https://github.com/Nirma/swift-evolution/commit/a51c543a76e9a1021996fb4b617311a588e7f397</a></font><br class=""></div><div class="gmail_default"><br class=""></div><div class="gmail_default"><font color="#274e13" face="comic sans ms, sans-serif" class="">Basically it boils down to something like this.</font></div><div class="gmail_default"><font color="#274e13" face="comic sans ms, sans-serif" class=""><br class=""></font></div><div class="gmail_default"><pre style="box-sizing:border-box;font-family:sfmono-regular,consolas,"liberation mono",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)" class=""><span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">public</span> <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">func</span> <span class="gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">clamp</span><<span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">T</span> : <span class="gmail-pl-e" style="box-sizing:border-box;color:rgb(121,93,163)"><span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Comparable</span></span>>(<span class="gmail-pl-smi" style="box-sizing:border-box;color:rgb(51,51,51)"><span class="gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">value</span></span>: T, <span class="gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">_</span> <span class="gmail-pl-smi" style="box-sizing:border-box;color:rgb(51,51,51)">lower</span>: T, <span class="gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">_</span> <span class="gmail-pl-smi" style="box-sizing:border-box;color:rgb(51,51,51)">upper</span>: T) <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">-></span> T {
<span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">return</span> <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">max</span>(lower, <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">min</span>(value, upper))
}</pre></div><div class="gmail_default"><br class=""></div><div class="gmail_default"><br class=""></div><div class="gmail_default"><font color="#274e13" face="comic sans ms, sans-serif" class="">What does the community think?</font></div></div></div></blockquote><br class=""></div><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;" class=""><div><div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(186, 45, 162);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Range</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {</span></div></div><div><div style="margin: 0px; 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: #ba2da2" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> clamp(</span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">_</span><span style="font-variant-ligatures: no-common-ligatures" class=""> x: </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">Bound</span><span style="font-variant-ligatures: no-common-ligatures" class="">) -> </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">Bound</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div></div><div><div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">return</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81" class="">max</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span><span style="font-variant-ligatures: no-common-ligatures" class="">lowerBound</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">, </span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81" class="">min</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(x, </span><span style="font-variant-ligatures: no-common-ligatures" class="">upperBound</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">))</span></div></div><div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div></div><div><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div></div></blockquote><br class=""><div class="">- Dave Sweeris</div></body></html>