<div dir="ltr"><div class="gmail_default" style="font-family:"comic sans ms",sans-serif;color:rgb(39,78,19)">Nevin,</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)">Yeah I think this works well as an extension on `<span style="font-family:monospace,monospace;font-size:12.8px;color:rgb(34,34,34)">Comparable</span>`, `foo.clamped(to: 1...100)` seems pretty natural.</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)">Why not go one step further and move the versions of min, max that take two arguments on over to `Comparable` as a protocol extension?</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)">Perhaps something like this?</div><div class="gmail_default" style="font-family:"comic sans ms",sans-serif;color:rgb(39,78,19)"><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div class="gmail_default" style="font-family:"comic sans ms",sans-serif;color:rgb(39,78,19)"><br></div><div class="gmail_default"><div class="gmail_default"><font color="#274e13" face="comic sans ms, sans-serif">extension Comparable {</font></div></div><div class="gmail_default"><div class="gmail_default"><font color="#274e13" face="comic sans ms, sans-serif"><br></font></div></div><div class="gmail_default"><div class="gmail_default"><font color="#274e13" face="comic sans ms, sans-serif"> func max(with value: Self) -> Self {</font></div></div><div class="gmail_default"><div class="gmail_default"><font color="#274e13" face="comic sans ms, sans-serif"> if value > self {</font></div></div><div class="gmail_default"><div class="gmail_default"><font color="#274e13" face="comic sans ms, sans-serif"> return value</font></div></div><div class="gmail_default"><div class="gmail_default"><font color="#274e13" face="comic sans ms, sans-serif"> }</font></div></div><div class="gmail_default"><div class="gmail_default"><font color="#274e13" face="comic sans ms, sans-serif"> return self</font></div></div><div class="gmail_default"><div class="gmail_default"><font color="#274e13" face="comic sans ms, sans-serif"> }</font></div></div><div class="gmail_default"><div class="gmail_default"><font color="#274e13" face="comic sans ms, sans-serif"><br></font></div></div><div class="gmail_default"><div class="gmail_default"><font color="#274e13" face="comic sans ms, sans-serif"> func min(with value: Self) -> Self {</font></div></div><div class="gmail_default"><div class="gmail_default"><font color="#274e13" face="comic sans ms, sans-serif"> if value < self {</font></div></div><div class="gmail_default"><div class="gmail_default"><font color="#274e13" face="comic sans ms, sans-serif"> return value</font></div></div><div class="gmail_default"><div class="gmail_default"><font color="#274e13" face="comic sans ms, sans-serif"> }</font></div></div><div class="gmail_default"><div class="gmail_default"><font color="#274e13" face="comic sans ms, sans-serif"> return self</font></div></div><div class="gmail_default"><div class="gmail_default"><font color="#274e13" face="comic sans ms, sans-serif"> }</font></div></div><div class="gmail_default"><div class="gmail_default"><font color="#274e13" face="comic sans ms, sans-serif"><br></font></div></div><div class="gmail_default"><div class="gmail_default"><font color="#274e13" face="comic sans ms, sans-serif"> func clamped(to range: ClosedRange<Self>) -> Self {</font></div></div><div class="gmail_default"><div class="gmail_default"><font color="#274e13" face="comic sans ms, sans-serif"> let selfUpperMin = range.upperBound.min(with: self)</font></div></div><div class="gmail_default"><div class="gmail_default"><font color="#274e13" face="comic sans ms, sans-serif"> return range.lowerBound.max(with: selfUpperMin)</font></div></div><div class="gmail_default"><div class="gmail_default"><font color="#274e13" face="comic sans ms, sans-serif"> }</font></div></div><div class="gmail_default"><div class="gmail_default"><font color="#274e13" face="comic sans ms, sans-serif">}</font></div></div></blockquote></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><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Mar 10, 2017 at 1:41 PM, Nevin Brackett-Rozinsky via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</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">I’d be on board with an extension of Comparable so you could write “16.clamped(to: 0...10)”. Something along the lines of:<div><div><br></div><div><div><div><font face="monospace, monospace">extension Comparable {</font></div><div><font face="monospace, monospace"> func clamped(to range: ClosedRange<Self>) -> Self {</font></div><div><font face="monospace, monospace"> return max(range.lowerBound, min(self, range.upperBound))</font></div><div><font face="monospace, monospace"> }</font></div><div><font face="monospace, monospace">}</font></div></div><span class="HOEnZb"><font color="#888888"><div><br></div><div>Nevin</div></font></span></div></div></div>
<br>______________________________<wbr>_________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br>
<br></blockquote></div><br></div>