<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div><div style="direction: inherit;"><div><div style="direction: inherit;"><span style="background-color: rgba(255, 255, 255, 0);">There is already a `clamped` function for clamping one range to another and I feel that this function should be consistent with that one. As I see it, it can either be consistent in that it takes a Range as its argument, or in being an extension to Range. Probably something like either one of these:</span></div></div><div style="direction: inherit;"><span style="background-color: rgba(255, 255, 255, 0);"><br></span></div><div style="direction: inherit;"><div style="direction: inherit;"><span style="background-color: rgba(255, 255, 255, 0);">extension Comparable { // or something else?</span></div><div style="direction: inherit;"><span style="background-color: rgba(255, 255, 255, 0);"> func clamped(to range: Range<Self>) -> Self </span></div><div style="direction: inherit;"><span style="background-color: rgba(255, 255, 255, 0);">}</span></div><div style="direction: inherit;"><span style="background-color: rgba(255, 255, 255, 0);"><br></span></div><div style="direction: inherit;"><span style="background-color: rgba(255, 255, 255, 0);">extension Range { </span></div><div style="direction: inherit;"><span style="background-color: rgba(255, 255, 255, 0);"> func clamping(_ value: Bound) -> Bound</span></div><div style="direction: inherit;"><span style="background-color: rgba(255, 255, 255, 0);">}</span></div><div style="direction: inherit;"><span style="background-color: rgba(255, 255, 255, 0);"><br></span></div><div style="direction: inherit;"><span style="background-color: rgba(255, 255, 255, 0);">Involving a range is good because that means that ensuring that the bounds checking (lower < upper) is already taken care of.</span></div><div style="direction: inherit;"><br></div><div style="direction: inherit;">I do feel that this a useful addition to the standard library. But it is purely additive, so it should wait until after Phase 1.</div><div style="direction: inherit;"><br></div><div style="direction: inherit;">- David</div></div></div><br>Sent from my iPad</div><div><br>On 2 Sep 2016, at 18:55, Nicholas Maccharoli via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br><br></div><blockquote type="cite"><div><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>
</div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></body></html>