On some thought, Strideable rather than Comparable seems appropriate. Strideable types have some meaningful concept of distance between two values and would (or should, if they don't already?) include types like Date. I like Strideable.clamped(to:) a lot, and the parallels with Range.clamped(to:) make sense.<br><div class="gmail_quote"><div dir="ltr">On Fri, Sep 2, 2016 at 15:58 David Rönnqvist via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div 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><div dir="auto"><div><br>On 2 Sep 2016, at 18:55, Nicholas Maccharoli via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">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="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="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="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="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="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="color:rgb(64,120,192);text-decoration:none;background-color:transparent" target="_blank">Discussion thread topic for that proposal</a></p><h2 style="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 href="https://github.com/Nirma/swift-evolution/blob/clamp_proposal/proposals/NNNN-add-clamp-to-standard-library.md#motivation" style="color:rgb(64,120,192);text-decoration:none;float:left;padding-right:4px;line-height:1;background-color:transparent" target="_blank"></a>Motivation</h2><p style="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="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="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="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="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="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="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="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 href="https://github.com/Nirma/swift-evolution/blob/clamp_proposal/proposals/NNNN-add-clamp-to-standard-library.md#proposed-solution" style="color:rgb(64,120,192);text-decoration:none;float:left;padding-right:4px;line-height:1;background-color:transparent" target="_blank"></a>Proposed solution</h2><p style="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="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="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="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="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 href="https://github.com/Nirma/swift-evolution/blob/clamp_proposal/proposals/NNNN-add-clamp-to-standard-library.md#detailed-design" style="color:rgb(64,120,192);text-decoration:none;float:left;padding-right:4px;line-height:1;background-color:transparent" target="_blank"></a>Detailed design</h2><p style="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 style="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="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 style="color:rgb(167,29,93)">public</span> <span style="color:rgb(167,29,93)">func</span> <span style="color:rgb(121,93,163)">clamp</span><T <span style="color:rgb(167,29,93)">:</span> <span style="color:rgb(0,134,179)">Comparable</span>>(value: T, <span style="color:rgb(121,93,163)">_</span> <span>lower</span>: T, <span style="color:rgb(121,93,163)">_</span> <span>upper</span>: T) <span style="color:rgb(167,29,93)">-></span> T {
<span style="color:rgb(167,29,93)">return</span> <span style="color:rgb(0,134,179)">max</span>(lower, <span style="color:rgb(0,134,179)">min</span>(value, upper))
}</pre></div><h2 style="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 href="https://github.com/Nirma/swift-evolution/blob/clamp_proposal/proposals/NNNN-add-clamp-to-standard-library.md#impact-on-existing-code" style="color:rgb(64,120,192);text-decoration:none;float:left;padding-right:4px;line-height:1;background-color:transparent" target="_blank"></a>Impact on existing code</h2><p style="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="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 href="https://github.com/Nirma/swift-evolution/blob/clamp_proposal/proposals/NNNN-add-clamp-to-standard-library.md#alternatives-considered" style="color:rgb(64,120,192);text-decoration:none;float:left;padding-right:4px;line-height:1;background-color:transparent" target="_blank"></a>Alternatives considered</h2><p style="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" target="_blank">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></div>_______________________________________________<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" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</blockquote></div>