<div dir="ltr"><div class="gmail_default" style="font-family:"comic sans ms",sans-serif;color:rgb(39,78,19)">As a side note, if anyone is interested the proposal markdown can be cloned from the fork I made of the swift-evolution repository here:</div><div class="gmail_default" style="font-family:"comic sans ms",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/blob/clamp_function/proposals/NNNN-add-clamp-function.md">https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md</a></font><br></div><div class="gmail_default"><font color="#274e13" face="comic sans ms, sans-serif"><br></font></div><div class="gmail_default"><font color="#274e13" face="comic sans ms, sans-serif">- Nick </font></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Mar 13, 2017 at 10:21 AM, Nicholas Maccharoli <span dir="ltr"><<a href="mailto:nmaccharoli@gmail.com" target="_blank">nmaccharoli@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_default" style="font-family:"comic sans ms",sans-serif;color:rgb(39,78,19)"><span class=""><div class="gmail_default" style="font-size:12.8px">Jaden,</div><div class="gmail_default" style="font-size:12.8px"><br></div><div class="gmail_default" style="font-size:12.8px">Awesome feedback, and yes I totally agree that support for Half-Open ranges would be awesome so I updated the proposal adding your code and also updating the example code in the motivation to include the Half-Open range variant and updating the "Alternatives Considered" section to accurately reflect that no alternatives have been considered. </div><div class="gmail_default" style="font-size:12.8px"><br></div><div class="gmail_default" style="font-size:12.8px">As for the implementation, an implementation using min-max would also be fine but I thought although a little verbose using an if-if-else-else clause would read a little easier.</div><div class="gmail_default" style="font-size:12.8px"><br></div><div class="gmail_default" style="font-size:12.8px">I personally think either way is great, just thought I would try an alternative.</div><div class="gmail_default" style="font-size:12.8px"><br></div><div class="gmail_default" style="font-size:12.8px">- Nick</div><div class="gmail_default" style="font-size:12.8px"><br></div></span><div class="gmail_default" style="font-size:12.8px"><h1 style="box-sizing:border-box;margin:0px 0px 16px;line-height:1.25;padding-bottom:0.3em;border-bottom:1px solid rgb(234,236,239);color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol"">Add clamp(to:) to the stdlib</h1><span class=""><ul style="box-sizing:border-box;padding-left:2em;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px"><li style="box-sizing:border-box">Proposal: <a href="https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-filename.md" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none" target="_blank">SE-NNNN</a></li><li style="box-sizing:border-box;margin-top:0.25em">Authors: <a href="https://github.com/Nirma" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none" target="_blank">Nicholas Maccharoli</a></li><li style="box-sizing:border-box;margin-top:0.25em">Review Manager: TBD</li><li style="box-sizing:border-box;margin-top:0.25em">Status: <span style="box-sizing:border-box;font-weight:600">Awaiting review</span></li></ul><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px"><em style="box-sizing:border-box">During the review process, add the following fields as needed:</em></p><ul style="box-sizing:border-box;padding-left:2em;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px"><li style="box-sizing:border-box">Decision Notes: <a href="https://lists.swift.org/pipermail/swift-evolution/" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none" target="_blank">Rationale</a>, <a href="https://lists.swift.org/pipermail/swift-evolution/" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none" target="_blank">Additional Commentary</a></li><li style="box-sizing:border-box;margin-top:0.25em">Bugs: <a href="https://bugs.swift.org/browse/SR-NNNN" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none" target="_blank">SR-NNNN</a>, <a href="https://bugs.swift.org/browse/SR-MMMM" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none" target="_blank">SR-MMMM</a></li><li style="box-sizing:border-box;margin-top:0.25em">Previous Revision: <a href="https://github.com/apple/swift-evolution/blob/...commit-ID.../proposals/NNNN-filename.md" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none" target="_blank">1</a></li><li style="box-sizing:border-box;margin-top:0.25em">Previous Proposal: <a href="https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/XXXX-filename.md" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none" target="_blank">SE-XXXX</a></li></ul><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(234,236,239);color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol""><a id="m_4764239902642338017gmail-user-content-introduction" class="m_4764239902642338017gmail-anchor" href="https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#introduction" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"></a>Introduction</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px">This proposal aims to add functionality to the standard library for clamping a value to a <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-radius:3px">ClosedRange</code>. The proposed function would allow the user to specify a range to clamp a value to where if the value fell within the range, the value would be returned as is, if the value being clamped exceeded the upper or lower bound in value the value of the boundary the value exceeded would be returned.</p><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px">Swift-evolution thread: <a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20170306/thread.html#33674" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none" target="_blank">Add a <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-radius:3px">clamp</code> function to Algorithm.swift</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(234,236,239);color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol""><a id="m_4764239902642338017gmail-user-content-motivation" class="m_4764239902642338017gmail-anchor" href="https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#motivation" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"></a>Motivation</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px">There have been quite a few times in my professional and personal programming life where I reached for a <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-radius:3px">clamped</code>function and was disappointed it was not part of the standard library.</p><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px">Having functionality like <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-radius:3px">clamped(to:)</code> added to <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-radius:3px">Comparable</code> as a protocol extension would benefit users of the Swift language who wish to guarantee that a value is kept within bounds.</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(234,236,239);color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol""><a id="m_4764239902642338017gmail-user-content-proposed-solution" class="m_4764239902642338017gmail-anchor" href="https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#proposed-solution" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"></a>Proposed solution</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px">The solution proposed is simply that there be a <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-radius:3px">clamped(to:)</code> function added to the Swift Standard Library. The function would behave much like its name describes.</p><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px">Given a <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-radius:3px">clamped(to:)</code> function existed it could be called in the following way, yielding the results in the adjacent comments:</p></span><div class="m_4764239902642338017gmail-highlight m_4764239902642338017gmail-highlight-source-swift" style="box-sizing:border-box;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px"><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"><span class="m_4764239902642338017gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">var</span> foo <span class="m_4764239902642338017gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">=</span> <span class="m_4764239902642338017gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">100</span>
<span class=""><span class="m_4764239902642338017gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="m_4764239902642338017gmail-pl-c" style="box-sizing:border-box">//</span> Closed range variant</span>
<span class="m_4764239902642338017gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>
foo.<span class="m_4764239902642338017gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">clamped</span>(<span class="m_4764239902642338017gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">to</span>: <span class="m_4764239902642338017gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">0</span><span class="m_4764239902642338017gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">...</span><span class="m_4764239902642338017gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">50</span>) <span class="m_4764239902642338017gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="m_4764239902642338017gmail-pl-c" style="box-sizing:border-box">//</span> 50</span>
<span class="m_4764239902642338017gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>foo.<span class="m_4764239902642338017gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">clamped</span>(<span class="m_4764239902642338017gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">to</span>: <span class="m_4764239902642338017gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">200</span><span class="m_4764239902642338017gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">...</span><span class="m_4764239902642338017gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">300</span>) <span class="m_4764239902642338017gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="m_4764239902642338017gmail-pl-c" style="box-sizing:border-box">//</span> 200</span>
<span class="m_4764239902642338017gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>foo.<span class="m_4764239902642338017gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">clamped</span>(<span class="m_4764239902642338017gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">to</span>: <span class="m_4764239902642338017gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">0</span><span class="m_4764239902642338017gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">...</span><span class="m_4764239902642338017gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">150</span>) <span class="m_4764239902642338017gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="m_4764239902642338017gmail-pl-c" style="box-sizing:border-box">//</span> 100</span>
<span class="m_4764239902642338017gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>
<span class="m_4764239902642338017gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="m_4764239902642338017gmail-pl-c" style="box-sizing:border-box">//</span> Half-Open range variant</span>
<span class="m_4764239902642338017gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>
foo.<span class="m_4764239902642338017gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">clamped</span>(<span class="m_4764239902642338017gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">to</span>: <span class="m_4764239902642338017gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">0</span><span class="m_4764239902642338017gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">..<</span><span class="m_4764239902642338017gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">50</span>) <span class="m_4764239902642338017gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="m_4764239902642338017gmail-pl-c" style="box-sizing:border-box">//</span> 49</span>
<span class="m_4764239902642338017gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>foo.<span class="m_4764239902642338017gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">clamped</span>(<span class="m_4764239902642338017gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">to</span>: <span class="m_4764239902642338017gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">200</span><span class="m_4764239902642338017gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">..<</span><span class="m_4764239902642338017gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">300</span>) <span class="m_4764239902642338017gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="m_4764239902642338017gmail-pl-c" style="box-sizing:border-box">//</span> 200</span>
<span class="m_4764239902642338017gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span></span>foo.<span class="m_4764239902642338017gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">clamped</span>(<span class="m_4764239902642338017gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">to</span>: <span class="m_4764239902642338017gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">0</span><span class="m_4764239902642338017gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">..<</span><span class="m_4764239902642338017gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">150</span>) <span class="m_4764239902642338017gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="m_4764239902642338017gmail-pl-c" style="box-sizing:border-box">//</span> 100</span></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(234,236,239);color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol""><a id="m_4764239902642338017gmail-user-content-detailed-design" class="m_4764239902642338017gmail-anchor" href="https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#detailed-design" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"></a>Detailed design</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px">The implementation of <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-radius:3px">clamped(to:)</code> that is being proposed is composed of two protocol extensions; one protocol extension on <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-radius:3px">Comparable</code> and another on <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-radius:3px">Strideable</code>.</p><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px">The implementation for <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-radius:3px">clamped(to:)</code> as an extension to <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-radius:3px">Comparable</code> accepting a range of type <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-radius:3px">ClosedRange<Self></code>would look like the following:</p><span class=""><div class="m_4764239902642338017gmail-highlight m_4764239902642338017gmail-highlight-source-swift" style="box-sizing:border-box;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px"><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"><span class="m_4764239902642338017gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">extension</span> <span class="m_4764239902642338017gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)"><span class="m_4764239902642338017gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Comparable</span></span> {
<span class="m_4764239902642338017gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">func</span> <span class="m_4764239902642338017gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">clamped</span>(<span class="m_4764239902642338017gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">to</span> <span class="m_4764239902642338017gmail-pl-smi" style="box-sizing:border-box;color:rgb(51,51,51)">range</span>: <span class="m_4764239902642338017gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">ClosedRange</span><<span class="m_4764239902642338017gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Self</span>>) <span class="m_4764239902642338017gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">-></span> <span class="m_4764239902642338017gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Self</span> {
<span class="m_4764239902642338017gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">if</span> <span class="m_4764239902642338017gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">self</span> <span class="m_4764239902642338017gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">></span> range.<span class="m_4764239902642338017gmail-pl-smi" style="box-sizing:border-box;color:rgb(51,51,51)">upperBound</span> {
<span class="m_4764239902642338017gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">return</span> range.<span class="m_4764239902642338017gmail-pl-smi" style="box-sizing:border-box;color:rgb(51,51,51)">upperBound</span>
} <span class="m_4764239902642338017gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">else</span> <span class="m_4764239902642338017gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">if</span> <span class="m_4764239902642338017gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">self</span> <span class="m_4764239902642338017gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)"><</span> range.<span class="m_4764239902642338017gmail-pl-smi" style="box-sizing:border-box;color:rgb(51,51,51)">lowerBound</span> {
<span class="m_4764239902642338017gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">return</span> range.<span class="m_4764239902642338017gmail-pl-smi" style="box-sizing:border-box;color:rgb(51,51,51)">lowerBound</span>
} <span class="m_4764239902642338017gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">else</span> {
<span class="m_4764239902642338017gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">return</span> <span class="m_4764239902642338017gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">self</span>
}
}
}</pre></div></span><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px">The implementation of <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-radius:3px">clamped(to:)</code> as an extension on <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-radius:3px">Strideable</code> would be confined to cases where the stride is of type <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-radius:3px">Integer</code>. The implementation would be as follows:</p><span class=""><div class="m_4764239902642338017gmail-highlight m_4764239902642338017gmail-highlight-source-swift" style="box-sizing:border-box;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px"><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"><span class="m_4764239902642338017gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">extension</span> <span class="m_4764239902642338017gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)"><span class="m_4764239902642338017gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Strideable</span></span> <span class="m_4764239902642338017gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">where</span> <span class="m_4764239902642338017gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Stride</span><span class="m_4764239902642338017gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">:</span> <span class="m_4764239902642338017gmail-pl-e" style="box-sizing:border-box;color:rgb(121,93,163)"><span class="m_4764239902642338017gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Integer</span></span> {
<span class="m_4764239902642338017gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">func</span> <span class="m_4764239902642338017gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">clamped</span>(<span class="m_4764239902642338017gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">to</span> <span class="m_4764239902642338017gmail-pl-smi" style="box-sizing:border-box;color:rgb(51,51,51)">range</span>: <span class="m_4764239902642338017gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Range</span><<span class="m_4764239902642338017gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Self</span>>) <span class="m_4764239902642338017gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">-></span> <span class="m_4764239902642338017gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Self</span> {
<span class="m_4764239902642338017gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">return</span> <span class="m_4764239902642338017gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">clamped</span>(<span class="m_4764239902642338017gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">to</span>: range.<span class="m_4764239902642338017gmail-pl-smi" style="box-sizing:border-box;color:rgb(51,51,51)">lowerBound</span><span class="m_4764239902642338017gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">...</span>(range.<span class="m_4764239902642338017gmail-pl-smi" style="box-sizing:border-box;color:rgb(51,51,51)">uppe<wbr>rBound</span> <span class="m_4764239902642338017gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">-</span> <span class="m_4764239902642338017gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">1</span>))
}
}</pre></div></span><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(234,236,239);color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol""><a id="m_4764239902642338017gmail-user-content-source-compatibility" class="m_4764239902642338017gmail-anchor" href="https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#source-compatibility" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"></a>Source compatibility</h2><span class=""><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px">This feature is purely additive; it has no effect on source compatibility.</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(234,236,239);color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol""><a id="m_4764239902642338017gmail-user-content-effect-on-abi-stability" class="m_4764239902642338017gmail-anchor" href="https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#effect-on-abi-stability" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"></a>Effect on ABI stability</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px">This feature is purely additive; it has no effect on ABI stability.</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(234,236,239);color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol""><a id="m_4764239902642338017gmail-user-content-effect-on-api-resilience" class="m_4764239902642338017gmail-anchor" href="https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#effect-on-api-resilience" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"></a>Effect on API resilience</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px">The proposed function would become part of the API but 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(234,236,239);color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol""><a id="m_4764239902642338017gmail-user-content-alternatives-considered" class="m_4764239902642338017gmail-anchor" href="https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#alternatives-considered" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"></a>Alternatives considered</h2></span><p style="box-sizing:border-box;margin-top:0px;color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px;margin-bottom:0px">Aside from doing nothing, no other alternatives were considered.</p></div></div></div><div class="gmail_extra"><br><div class="gmail_quote"><span class="">On Mon, Mar 13, 2017 at 7:47 AM, Jaden Geller <span dir="ltr"><<a href="mailto:jaden.geller@gmail.com" target="_blank">jaden.geller@gmail.com</a>></span> wrote:<br></span><div><div class="h5"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">You definitely ought to add “do nothing” to the alternative considered section. Also, any reason you didn’t implement `clamped` in terms of `min` and `max`?<div><br><div><blockquote type="cite"><span><div>On Mar 12, 2017, at 10:16 AM, Nicholas Maccharoli via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:</div><br class="m_4764239902642338017m_6141688997429937384Apple-interchange-newline"></span><div><div><div class="m_4764239902642338017h5"><div dir="ltr"><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></div><div class="gmail_default" style="font-family:"comic sans ms",sans-serif;color:rgb(39,78,19)">This is a draft of a proposal adding `clamped(to:)` as an extension on `Comparable`.</div><div class="gmail_default" style="font-family:"comic sans ms",sans-serif;color:rgb(39,78,19)">For now I have only included a version of the `clamped(to:)` function that accepts `ClosedRange` but if the community decided that it is feasible to add a version that takes half open ranges I would also love to include that as well in this proposal. </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)">That you so much for the feedback so far and I am looking forward <br></div><div class="gmail_default" style="font-family:"comic sans ms",sans-serif;color:rgb(39,78,19)">to the community's feedback on this draft. </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)">- Nick </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)"><br></div><h1 style="box-sizing:border-box;margin:0px 0px 16px;line-height:1.25;padding-bottom:0.3em;border-bottom:1px solid rgb(234,236,239);color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol"">Add clamp<div class="gmail_default" style="font-family:"comic sans ms",sans-serif;color:rgb(39,78,19);display:inline">ed</div>(to:) to the stdlib</h1><ul style="box-sizing:border-box;padding-left:2em;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px"><li style="box-sizing:border-box">Proposal: <a href="https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-filename.md" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none" target="_blank">SE-NNNN</a></li><li style="box-sizing:border-box;margin-top:0.25em">Authors: <a href="https://github.com/Nirma" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none" target="_blank">Nicholas Maccharoli</a></li><li style="box-sizing:border-box;margin-top:0.25em">Review Manager: TBD</li><li style="box-sizing:border-box;margin-top:0.25em">Status: <span style="box-sizing:border-box;font-weight:600">Awaiting review</span></li></ul><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px"><em style="box-sizing:border-box">During the review process, add the following fields as needed:</em></p><ul style="box-sizing:border-box;padding-left:2em;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px"><li style="box-sizing:border-box">Decision Notes: <a href="https://lists.swift.org/pipermail/swift-evolution/" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none" target="_blank">Rationale</a>, <a href="https://lists.swift.org/pipermail/swift-evolution/" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none" target="_blank">Additional Commentary</a></li><li style="box-sizing:border-box;margin-top:0.25em">Bugs: <a href="https://bugs.swift.org/browse/SR-NNNN" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none" target="_blank">SR-NNNN</a>, <a href="https://bugs.swift.org/browse/SR-MMMM" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none" target="_blank">SR-MMMM</a></li><li style="box-sizing:border-box;margin-top:0.25em">Previous Revision: <a href="https://github.com/apple/swift-evolution/blob/...commit-ID.../proposals/NNNN-filename.md" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none" target="_blank">1</a></li><li style="box-sizing:border-box;margin-top:0.25em">Previous Proposal: <a href="https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/XXXX-filename.md" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none" target="_blank">SE-XXXX</a></li></ul><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(234,236,239);color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol""><a id="m_4764239902642338017m_6141688997429937384gmail-user-content-introduction" class="m_4764239902642338017m_6141688997429937384gmail-anchor" href="https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#introduction" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"></a>Introduction</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px">This proposal aims to add functionality to the standard library for clamping a value to a <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-radius:3px">ClosedRange</code>. The proposed function would allow the user to specify a range to clamp a value to where if the value fell within the range, the value would be returned as is, if the value being clamped exceeded the upper or lower bound in value the value of the boundary the value exceeded would be returned.</p><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px">Swift-evolution thread: <a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20170306/thread.html#33674" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none" target="_blank">Add a <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-radius:3px">clamp</code> function to Algorithm.swift</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(234,236,239);color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol""><a id="m_4764239902642338017m_6141688997429937384gmail-user-content-motivation" class="m_4764239902642338017m_6141688997429937384gmail-anchor" href="https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#motivation" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"></a>Motivation</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px">There have been quite a few times in my professional and personal programming life where I reached for a <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-radius:3px">clamped</code>function and was disappointed it was not part of the standard library.</p><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px">Having functionality like <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-radius:3px">clamped(to:)</code> added to <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-radius:3px">Comparable</code> as a protocol extension would benefit users of the Swift language who wish to guarantee that a value is kept within bounds.</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(234,236,239);color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol""><a id="m_4764239902642338017m_6141688997429937384gmail-user-content-proposed-solution" class="m_4764239902642338017m_6141688997429937384gmail-anchor" href="https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#proposed-solution" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"></a>Proposed solution</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px">The solution proposed is simply that there be a <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-radius:3px">clamped(to:)</code> function added to the Swift Standard Library. The function would behave much like its name describes.</p><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px">Given a <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-radius:3px">clamped(to:)</code> function existed it could be called in the following way, yielding the results in the adjacent comments:</p><div class="m_4764239902642338017m_6141688997429937384gmail-highlight m_4764239902642338017m_6141688997429937384gmail-highlight-source-swift" style="box-sizing:border-box;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px"><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"><span class="m_4764239902642338017m_6141688997429937384gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">var</span> foo <span class="m_4764239902642338017m_6141688997429937384gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">=</span> <span class="m_4764239902642338017m_6141688997429937384gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">100</span>
foo.<span class="m_4764239902642338017m_6141688997429937384gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">clamp</span>(<span class="m_4764239902642338017m_6141688997429937384gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">to</span>: <span class="m_4764239902642338017m_6141688997429937384gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">0</span><span class="m_4764239902642338017m_6141688997429937384gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">...</span><span class="m_4764239902642338017m_6141688997429937384gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">50</span>) <span class="m_4764239902642338017m_6141688997429937384gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="m_4764239902642338017m_6141688997429937384gmail-pl-c" style="box-sizing:border-box">//</span> 50</span>
<span class="m_4764239902642338017m_6141688997429937384gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>foo.<span class="m_4764239902642338017m_6141688997429937384gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">clamp</span>(<span class="m_4764239902642338017m_6141688997429937384gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">to</span>: <span class="m_4764239902642338017m_6141688997429937384gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">200</span><span class="m_4764239902642338017m_6141688997429937384gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">...</span><span class="m_4764239902642338017m_6141688997429937384gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">300</span>) <span class="m_4764239902642338017m_6141688997429937384gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="m_4764239902642338017m_6141688997429937384gmail-pl-c" style="box-sizing:border-box">//</span> 200</span>
<span class="m_4764239902642338017m_6141688997429937384gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"></span>foo.<span class="m_4764239902642338017m_6141688997429937384gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">clamp</span>(<span class="m_4764239902642338017m_6141688997429937384gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">to</span>: <span class="m_4764239902642338017m_6141688997429937384gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">0</span><span class="m_4764239902642338017m_6141688997429937384gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">...</span><span class="m_4764239902642338017m_6141688997429937384gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">150</span>) <span class="m_4764239902642338017m_6141688997429937384gmail-pl-c" style="box-sizing:border-box;color:rgb(150,152,150)"><span class="m_4764239902642338017m_6141688997429937384gmail-pl-c" style="box-sizing:border-box">//</span> 100</span></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(234,236,239);color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol""><a id="m_4764239902642338017m_6141688997429937384gmail-user-content-detailed-design" class="m_4764239902642338017m_6141688997429937384gmail-anchor" href="https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#detailed-design" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"></a>Detailed design</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px">The implementation for <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-radius:3px">clamped(to:)</code> as an extension to <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-radius:3px">Comparable</code> accepting a range of type <code style="box-sizing:border-box;font-family:sfmono-regular,consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-radius:3px">ClosedRange<Self></code>would look like the following:</p><div class="m_4764239902642338017m_6141688997429937384gmail-highlight m_4764239902642338017m_6141688997429937384gmail-highlight-source-swift" style="box-sizing:border-box;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px"><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"><span class="m_4764239902642338017m_6141688997429937384gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">extension</span> <span class="m_4764239902642338017m_6141688997429937384gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)"><span class="m_4764239902642338017m_6141688997429937384gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Comparable</span></span> {
<span class="m_4764239902642338017m_6141688997429937384gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">func</span> <span class="m_4764239902642338017m_6141688997429937384gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">clamped</span>(<span class="m_4764239902642338017m_6141688997429937384gmail-pl-en" style="box-sizing:border-box;color:rgb(121,93,163)">to</span> <span class="m_4764239902642338017m_6141688997429937384gmail-pl-smi" style="box-sizing:border-box;color:rgb(51,51,51)">range</span>: <span class="m_4764239902642338017m_6141688997429937384gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">ClosedRange</span><<span class="m_4764239902642338017m_6141688997429937384gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Self</span>>) <span class="m_4764239902642338017m_6141688997429937384gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">-></span> <span class="m_4764239902642338017m_6141688997429937384gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">Self</span> {
<span class="m_4764239902642338017m_6141688997429937384gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">if</span> <span class="m_4764239902642338017m_6141688997429937384gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">self</span> <span class="m_4764239902642338017m_6141688997429937384gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">></span> range.<span class="m_4764239902642338017m_6141688997429937384gmail-pl-smi" style="box-sizing:border-box;color:rgb(51,51,51)">upperBound</span> {
<span class="m_4764239902642338017m_6141688997429937384gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">return</span> range.<span class="m_4764239902642338017m_6141688997429937384gmail-pl-smi" style="box-sizing:border-box;color:rgb(51,51,51)">upperBound</span>
} <span class="m_4764239902642338017m_6141688997429937384gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">else</span> <span class="m_4764239902642338017m_6141688997429937384gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">if</span> <span class="m_4764239902642338017m_6141688997429937384gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">self</span> <span class="m_4764239902642338017m_6141688997429937384gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)"><</span> range.<span class="m_4764239902642338017m_6141688997429937384gmail-pl-smi" style="box-sizing:border-box;color:rgb(51,51,51)">lowerBound</span> {
<span class="m_4764239902642338017m_6141688997429937384gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">return</span> range.<span class="m_4764239902642338017m_6141688997429937384gmail-pl-smi" style="box-sizing:border-box;color:rgb(51,51,51)">lowerBound</span>
} <span class="m_4764239902642338017m_6141688997429937384gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">else</span> {
<span class="m_4764239902642338017m_6141688997429937384gmail-pl-k" style="box-sizing:border-box;color:rgb(167,29,93)">return</span> <span class="m_4764239902642338017m_6141688997429937384gmail-pl-c1" style="box-sizing:border-box;color:rgb(0,134,179)">self</span>
}
}
}</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(234,236,239);color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol""><a id="m_4764239902642338017m_6141688997429937384gmail-user-content-source-compatibility" class="m_4764239902642338017m_6141688997429937384gmail-anchor" href="https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#source-compatibility" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"></a>Source compatibility</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px">This feature is purely additive; it has no effect on source compatibility.</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(234,236,239);color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol""><a id="m_4764239902642338017m_6141688997429937384gmail-user-content-effect-on-abi-stability" class="m_4764239902642338017m_6141688997429937384gmail-anchor" href="https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#effect-on-abi-stability" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"></a>Effect on ABI stability</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px">This feature is purely additive; it has no effect on ABI stability.</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(234,236,239);color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol""><a id="m_4764239902642338017m_6141688997429937384gmail-user-content-effect-on-api-resilience" class="m_4764239902642338017m_6141688997429937384gmail-anchor" href="https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#effect-on-api-resilience" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"></a>Effect on API resilience</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px">The proposed function would become part of the API but 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(234,236,239);color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol""><a id="m_4764239902642338017m_6141688997429937384gmail-user-content-alternatives-considered" class="m_4764239902642338017m_6141688997429937384gmail-anchor" href="https://github.com/Nirma/swift-evolution/blob/clamp_function/proposals/NNNN-add-clamp-function.md#alternatives-considered" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"></a>Alternatives considered</h2><div class="gmail_default" style="font-family:"comic sans ms",sans-serif;color:rgb(39,78,19)"><code style="color:rgb(36,41,46);box-sizing:border-box;font-family:sfmono-regular,consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-radius:3px">clamped(to:)</code><span style="color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px"> </span><span style="color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px">could be made a global function like</span><span style="color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px"> </span><code style="color:rgb(36,41,46);box-sizing:border-box;font-family:sfmono-regular,consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-radius:3px">min</code><span style="color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px"> </span><span style="color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px">and</span><span style="color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px"> </span><code style="color:rgb(36,41,46);box-sizing:border-box;font-family:sfmono-regular,consolas,"liberation mono",menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-radius:3px">max</code><span style="color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px"> </span><span style="color:rgb(36,41,46);font-family:-apple-system,system-ui,"segoe ui",helvetica,arial,sans-serif,"apple color emoji","segoe ui emoji","segoe ui symbol";font-size:16px">but a protocol extension seems like a better choice.</span></div></div></div></div><span>
______________________________<wbr>_________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailma<wbr>n/listinfo/swift-evolution</a><br></span></div></blockquote></div><br></div></div></blockquote></div></div></div><br></div>
</blockquote></div><br></div>