<div dir="ltr"><div class="gmail_default" style="font-family:"comic sans ms",sans-serif;color:rgb(39,78,19)"><div class="gmail_default" style="font-size:12.8px">Sorry for sidetracking by talking about dumping the global definitions of `min` and `max` but if that could be done and it were decided by the swift community that adding a clamp function would be appropriate, I guess with the array implementations of min / max the clamp function might be implemented like this?</div><div class="gmail_default" style="font-size:12.8px"><br></div><div class="gmail_default" style="font-size:12.8px"><p class="gmail-m_8093425548888026859gmail-p1"><span class="gmail-m_8093425548888026859gmail-s1">extension</span><span class="gmail-m_8093425548888026859gmail-s2"> </span><span class="gmail-m_8093425548888026859gmail-s3">Comparable</span><span class="gmail-m_8093425548888026859gmail-s2"> {</span></p><p class="gmail-m_8093425548888026859gmail-p2"><span class="gmail-m_8093425548888026859gmail-s3"> </span><span class="gmail-m_8093425548888026859gmail-s1">func</span><span class="gmail-m_8093425548888026859gmail-s3"> clamped(to range: </span><span class="gmail-m_8093425548888026859gmail-s4">ClosedRange</span><span class="gmail-m_8093425548888026859gmail-s3"><</span><span class="gmail-m_8093425548888026859gmail-s5">Self</span><span class="gmail-m_8093425548888026859gmail-s3">>) -> </span><span class="gmail-m_8093425548888026859gmail-s5">Self</span><span class="gmail-m_8093425548888026859gmail-s3"> {</span></p><p class="gmail-m_8093425548888026859gmail-p2"><span class="gmail-m_8093425548888026859gmail-s3"> </span><span class="gmail-m_8093425548888026859gmail-s1">return</span><span class="gmail-m_8093425548888026859gmail-s3"> [range.</span><span class="gmail-m_8093425548888026859gmail-s4">lowerBound</span><span class="gmail-m_8093425548888026859gmail-s3">, [</span><span class="gmail-m_8093425548888026859gmail-s1">self</span><span class="gmail-m_8093425548888026859gmail-s3">, range.</span><span class="gmail-m_8093425548888026859gmail-s4">upperBound</span><span class="gmail-m_8093425548888026859gmail-s3">].</span><span class="gmail-m_8093425548888026859gmail-s4">min</span><span class="gmail-m_8093425548888026859gmail-s3">()!].</span><span class="gmail-m_8093425548888026859gmail-s4">max</span><span class="gmail-m_8093425548888026859gmail-s3">(<wbr>)!</span></p><p class="gmail-m_8093425548888026859gmail-p2"><span class="gmail-m_8093425548888026859gmail-s3"> }</span></p><p class="gmail-m_8093425548888026859gmail-p2"><span class="gmail-m_8093425548888026859gmail-s3">}</span></p></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Mar 10, 2017 at 5:22 PM, Jaden Geller <span dir="ltr"><<a href="mailto:jaden.geller@gmail.com" target="_blank">jaden.geller@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 style="word-wrap:break-word"><br><div><span class=""><blockquote type="cite"><div>On Mar 9, 2017, at 11:20 PM, Nicholas Maccharoli via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:</div><br class="m_5105699372457732462Apple-interchange-newline"><div><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></div></blockquote><div><br></div></span><div>I think that a symmetric operation like `min` or `max` ought to treat both arguments in a symmetric way. `3.max(with: 9)` not only reads badly, but privileges one argument over the other syntactically. I’d very much like to avoid this.</div><div><br></div><div>I would be okay with removing top-level min and max if `Array` min and max could generate equivalent code given an array literal. This seems possible.</div><div><div class="h5"><br><blockquote type="cite"><div><div dir="ltr"><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="m_5105699372457732462HOEnZb"><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" 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/mailma<wbr>n/listinfo/swift-evolution</a><br>
<br></blockquote></div><br></div>
______________________________<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/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br></div></blockquote></div></div></div><br></div></blockquote></div><br></div>