<div dir="ltr"><div class="gmail_extra"><div><div class="m_-3404327379327894526gmail_signature"><div dir="ltr"><div>On Sun, Jan 15, 2017 at 6:13 PM, Xiaodi Wu via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br></div></div></div></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div>One example: earlier, it was demonstrated that a genetic lerp would not accommodate vector types. However, it _does_ work fine for any scalar (i.e. field) type; however, with the currently proposed integer protocols, one would constrain it to Arithmetic types, yet the algorithm would be bogus for integers.</div></blockquote><div><br></div><div>I wouldn&#39;t say lerp&lt;Int&gt; is bogus (the results aren&#39;t wrong), just much less useful.</div><div><br></div><div>On the other hand:</div><div><br></div><div><font face="monospace, monospace">    extension Collection where Iterator.Element: Arithmetic {</font></div><div><font face="monospace, monospace">        func mean() -&gt; Iterator.Element {</font></div><div><font face="monospace, monospace">            return self.reduce(Iterator.Element()<wbr>, +) / Iterator.Element(count)  // assuming these initializers exist</font></div><div><font face="monospace, monospace">        }</font></div><div><font face="monospace, monospace">    }</font></div><div> </div><div>Now [6, 7].mean() returns 6, and <b>that</b> I would call bogus.</div><div><br></div><div>Of course, there are some alternative ways to implement this which avoid the issue:</div><div><br></div><div>- Implement it only for Iterator.Element: FloatingPoint.</div><div>- Implement it as mean&lt;T: FloatingPoint&gt;() -&gt; T, reducing by { $0 + T($1) } or whatever the appropriate conversion initializer is, or simply converting to floating point once <i>after</i> taking the sum.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class="m_-3404327379327894526gmail-HOEnZb"><div class="m_-3404327379327894526gmail-h5"><div><br></div><div><br><div class="gmail_quote"><div>On Sun, Jan 15, 2017 at 19:21 Dave Abrahams &lt;<a href="mailto:dabrahams@apple.com" target="_blank">dabrahams@apple.com</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>on Sun Jan 15 2017, Xiaodi Wu &lt;<a href="http://xiaodi.wu-AT-gmail.com" rel="noreferrer" class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg" target="_blank">xiaodi.wu-AT-gmail.com</a>&gt; wrote:<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br><br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt; On Sun, Jan 15, 2017 at 3:27 PM, Dave Abrahams via swift-evolution &lt;<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt; <a href="mailto:swift-evolution@swift.org" class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg" target="_blank">swift-evolution@swift.org</a>&gt; wrote:<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt;<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt;&gt;<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt;&gt; on Sun Jan 15 2017, Xiaodi Wu &lt;<a href="mailto:swift-evolution@swift.org" class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg" target="_blank">swift-evolution@swift.org</a>&gt; wrote:<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt;&gt;<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt;&gt; &gt; There _may_ be value in recognizing the distinction between rings and<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt;&gt; &gt; fields, perhaps? Just as the FP protocols make room for people to<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt;&gt; implement<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt;&gt; &gt; their own decimal FP types, and just as you&#39;re trying to make Arithmetic<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt;&gt; &gt; accommodate complex numbers, the distinction would allow someone to write<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt;&gt; &gt; algorithms generic over rationals and reals (i.e. fields). Being able to<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt;&gt; &gt; represent exact fractions isn&#39;t so terribly niche, and I think the design<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt;&gt; &gt; wouldn&#39;t be terribly complicated by its accommodation:<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt;&gt; &gt;<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt;&gt; &gt; ```<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt;&gt; &gt; // rename Arithmetic to Ring<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt;&gt; &gt; // it&#39;s acceptable to omit `.one` from Ring, though some may call that a<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt;&gt; &gt; Pseudoring<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt;&gt; &gt; // consider omitting division from Ring and pushing it down to<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt;&gt; &gt; BinaryInteger and Field<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt;&gt; &gt;<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt;&gt; &gt; protocol BinaryInteger : Ring { ... }<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt;&gt; &gt;<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt;&gt; &gt; protocol Field : Ring {<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt;&gt; &gt;   static var one { get }<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt;&gt; &gt;   static func / (Self, Self) -&gt; Self<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt;&gt; &gt;   static func /= (inout Self, Self)<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt;&gt; &gt;   var inverted: Self { get } // default impl: .one / self<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt;&gt; &gt; }<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt;&gt; &gt;<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt;&gt; &gt; protocol FloatingPoint : Field { ... }<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt;&gt; &gt; // rational number types and complex number types<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt;&gt; &gt; // would also conform to Field<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt;&gt; &gt; ```<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt;&gt;<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt;&gt; What generic algorithms would this enable?<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt;<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt; For one, anything to do with dividing into equal parts<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br><br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>For example...?<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br><br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt; could be generic over floating point, rational, and even complex<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt; numbers, but you probably wouldn&#39;t want to include integer types in<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt; such an algorithm.<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt;<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt;&gt; Would they be appropriate<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt;&gt; for the standard library (as opposed to some more specialized numerics<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt;&gt; library)?<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt;&gt;<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt;<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt; The issue is that it&#39;s not terribly ergonomic to relegate `Field` to a<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt; specialized library because one cannot retroactively conform<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>&gt; `FloatingPoint` to `Field`.<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br><br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>I don&#39;t think this is an important enough concern to justify adding<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>protocols to the standard library.  The number of types one has to<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>individually make conform to Field is probably going to remain small.<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br><br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>Show-me-the-mone^Walgorithms-l<wbr>y y&#39;rs,<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br><br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>--<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br>-Dave<br class="m_-3404327379327894526gmail-m_-925663533859349757gmail_msg"><br></blockquote></div></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></div>