<div dir="ltr"><div class="gmail_default" style="font-family:"comic sans ms",sans-serif;color:rgb(39,78,19)"><h2 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom:1px solid rgb(238,238,238);color:rgb(51,51,51);font-family:-apple-system,blinkmacsystemfont,"segoe ui",roboto,helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol"">Introduction</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:-apple-system,blinkmacsystemfont,"segoe ui",roboto,helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px;line-height:24px">The aim of this proposal is to introduce <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">clamp</code> functionality to the swift standard library. The <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">clamp</code> function would take a value and a upper and lower bounds to <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">clamp</code> the value to, ensuring that the return value of the function is within the range specified by the upper and lower bounds. Swift-evolution thread: <a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160829/026748.html" style="box-sizing:border-box;color:rgb(64,120,192);text-decoration:none;background-color:transparent">Discussion thread topic for that proposal</a></p><h2 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom:1px solid rgb(238,238,238);color:rgb(51,51,51);font-family:-apple-system,blinkmacsystemfont,"segoe ui",roboto,helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol""><a id="gmail-user-content-motivation" class="gmail-anchor" href="https://github.com/Nirma/swift-evolution/blob/clamp_proposal/proposals/NNNN-add-clamp-to-standard-library.md#motivation" style="box-sizing:border-box;color:rgb(64,120,192);text-decoration:none;float:left;padding-right:4px;line-height:1;background-color:transparent"></a>Motivation</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:-apple-system,blinkmacsystemfont,"segoe ui",roboto,helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px;line-height:24px">The standard library already includes functionality for <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">min</code> and <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">max</code> but at the time of this writing there is no global function for <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">clamp</code>. Adding a <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">clamp</code> function would complement <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">min</code> and <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">max</code> really well and would also eliminate the need for developers to write their own clamp functionality.</p><h2 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom:1px solid rgb(238,238,238);color:rgb(51,51,51);font-family:-apple-system,blinkmacsystemfont,"segoe ui",roboto,helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol""><a id="gmail-user-content-proposed-solution" class="gmail-anchor" href="https://github.com/Nirma/swift-evolution/blob/clamp_proposal/proposals/NNNN-add-clamp-to-standard-library.md#proposed-solution" style="box-sizing:border-box;color:rgb(64,120,192);text-decoration:none;float:left;padding-right:4px;line-height:1;background-color:transparent"></a>Proposed solution</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:-apple-system,blinkmacsystemfont,"segoe ui",roboto,helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px;line-height:24px">I propose that a global <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">clamp</code> function akin to <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">min</code> and <code style="box-sizing:border-box;font-family:consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">max</code> be added to the standard library.</p><h2 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom:1px solid rgb(238,238,238);color:rgb(51,51,51);font-family:-apple-system,blinkmacsystemfont,"segoe ui",roboto,helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol""><a id="gmail-user-content-detailed-design" class="gmail-anchor" href="https://github.com/Nirma/swift-evolution/blob/clamp_proposal/proposals/NNNN-add-clamp-to-standard-library.md#detailed-design" style="box-sizing:border-box;color:rgb(64,120,192);text-decoration:none;float:left;padding-right:4px;line-height:1;background-color:transparent"></a>Detailed design</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:-apple-system,blinkmacsystemfont,"segoe ui",roboto,helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px;line-height:24px">The implementation of the global clamp function would be as follows:</p><div class="gmail-highlight gmail-highlight-source-swift" style="box-sizing:border-box;margin-bottom:16px;color:rgb(51,51,51);font-family:-apple-system,blinkmacsystemfont,"segoe ui",roboto,helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px;line-height:24px"><pre style="box-sizing:border-box;font-family: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;border-radius:3px;word-break:normal;background-color:rgb(247,247,247)"><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><T <span class="gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">:</span> <span class="gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Comparable</span>>(value: 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">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">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><h2 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom:1px solid rgb(238,238,238);color:rgb(51,51,51);font-family:-apple-system,blinkmacsystemfont,"segoe ui",roboto,helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol""><a id="gmail-user-content-impact-on-existing-code" class="gmail-anchor" href="https://github.com/Nirma/swift-evolution/blob/clamp_proposal/proposals/NNNN-add-clamp-to-standard-library.md#impact-on-existing-code" style="box-sizing:border-box;color:rgb(64,120,192);text-decoration:none;float:left;padding-right:4px;line-height:1;background-color:transparent"></a>Impact on existing code</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:-apple-system,blinkmacsystemfont,"segoe ui",roboto,helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px;line-height:24px">There are no impacts on existing code, this proposal is purely additive.</p><h2 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom:1px solid rgb(238,238,238);color:rgb(51,51,51);font-family:-apple-system,blinkmacsystemfont,"segoe ui",roboto,helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol""><a id="gmail-user-content-alternatives-considered" class="gmail-anchor" href="https://github.com/Nirma/swift-evolution/blob/clamp_proposal/proposals/NNNN-add-clamp-to-standard-library.md#alternatives-considered" style="box-sizing:border-box;color:rgb(64,120,192);text-decoration:none;float:left;padding-right:4px;line-height:1;background-color:transparent"></a>Alternatives considered</h2><p style="box-sizing:border-box;margin-top:0px;color:rgb(51,51,51);font-family:-apple-system,blinkmacsystemfont,"segoe ui",roboto,helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px;line-height:24px;margin-bottom:0px">Aside from not making the additions that this proposal wishes to make to the standard library no other alternatives have been considered.</p></div></div>