<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Mar 9, 2017, at 11:20 PM, Nicholas Maccharoli via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><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 class=""></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)" class="">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 class=""></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 class=""></div><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 class=""></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><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class="gmail_default" style="font-family:"comic sans ms",sans-serif;color:rgb(39,78,19)"><br class=""></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" class=""><div class="gmail_default" style="font-family:"comic sans ms",sans-serif;color:rgb(39,78,19)"><br class=""></div><div class="gmail_default"><div class="gmail_default"><font color="#274e13" face="comic sans ms, sans-serif" class="">extension Comparable {</font></div></div><div class="gmail_default"><div class="gmail_default"><font color="#274e13" face="comic sans ms, sans-serif" class=""><br class=""></font></div></div><div class="gmail_default"><div class="gmail_default"><font color="#274e13" face="comic sans ms, sans-serif" class=""> 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" class=""> if value > self {</font></div></div><div class="gmail_default"><div class="gmail_default"><font color="#274e13" face="comic sans ms, sans-serif" class=""> return value</font></div></div><div class="gmail_default"><div class="gmail_default"><font color="#274e13" face="comic sans ms, sans-serif" class=""> }</font></div></div><div class="gmail_default"><div class="gmail_default"><font color="#274e13" face="comic sans ms, sans-serif" class=""> return self</font></div></div><div class="gmail_default"><div class="gmail_default"><font color="#274e13" face="comic sans ms, sans-serif" class=""> }</font></div></div><div class="gmail_default"><div class="gmail_default"><font color="#274e13" face="comic sans ms, sans-serif" class=""><br class=""></font></div></div><div class="gmail_default"><div class="gmail_default"><font color="#274e13" face="comic sans ms, sans-serif" class=""> 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" class=""> if value < self {</font></div></div><div class="gmail_default"><div class="gmail_default"><font color="#274e13" face="comic sans ms, sans-serif" class=""> return value</font></div></div><div class="gmail_default"><div class="gmail_default"><font color="#274e13" face="comic sans ms, sans-serif" class=""> }</font></div></div><div class="gmail_default"><div class="gmail_default"><font color="#274e13" face="comic sans ms, sans-serif" class=""> return self</font></div></div><div class="gmail_default"><div class="gmail_default"><font color="#274e13" face="comic sans ms, sans-serif" class=""> }</font></div></div><div class="gmail_default"><div class="gmail_default"><font color="#274e13" face="comic sans ms, sans-serif" class=""><br class=""></font></div></div><div class="gmail_default"><div class="gmail_default"><font color="#274e13" face="comic sans ms, sans-serif" class=""> 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" class=""> 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" class=""> 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" class=""> }</font></div></div><div class="gmail_default"><div class="gmail_default"><font color="#274e13" face="comic sans ms, sans-serif" class="">}</font></div></div></blockquote></div><div class="gmail_default" style="font-family:"comic sans ms",sans-serif;color:rgb(39,78,19)"><br class=""></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 class=""></div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Fri, Mar 10, 2017 at 1:41 PM, Nevin Brackett-Rozinsky via swift-evolution <span dir="ltr" class=""><<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr" class="">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 class=""><div class=""><br class=""></div><div class=""><div class=""><div class=""><font face="monospace, monospace" class="">extension Comparable {</font></div><div class=""><font face="monospace, monospace" class=""> func clamped(to range: ClosedRange<Self>) -> Self {</font></div><div class=""><font face="monospace, monospace" class=""> return max(range.lowerBound, min(self, range.upperBound))</font></div><div class=""><font face="monospace, monospace" class=""> }</font></div><div class=""><font face="monospace, monospace" class="">}</font></div></div><span class="HOEnZb"><font color="#888888" class=""><div class=""><br class=""></div><div class="">Nevin</div></font></span></div></div></div>
<br class="">______________________________<wbr class="">_________________<br class="">
swift-evolution mailing list<br class="">
<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/<wbr class="">mailman/listinfo/swift-<wbr class="">evolution</a><br class="">
<br class=""></blockquote></div><br class=""></div>
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></body></html>