<div dir="ltr">Using a sequence meant to represent a [Cartesian Product](<a href="https://github.com/griotspeak/CartesianProduct">https://github.com/griotspeak/CartesianProduct</a>)<div><br></div><div>You can write this</div><div><br></div><div><p style="margin:0px;font-size:11px;font-family:Menlo">        <span style="color:rgb(187,44,162)">for</span> (i, j) <span style="color:rgb(187,44,162)">in</span> (<span style="color:rgb(39,42,216)">0</span>...<span style="color:rgb(39,42,216)">10</span>) <span style="color:rgb(49,89,93)">•</span> (<span style="color:rgb(39,42,216)">0</span>...<span style="color:rgb(39,42,216)">10</span>) <span style="color:rgb(187,44,162)">where</span> i + j &lt; <span style="color:rgb(39,42,216)">8</span> {</p>
<p style="margin:0px;font-size:11px;font-family:Menlo">            <span style="color:rgb(61,29,129)">print</span>(i)</p>
<p style="margin:0px;font-size:11px;font-family:Menlo">        }</p><p style="margin:0px;font-size:11px;font-family:Menlo"><br></p><p style="margin:0px;font-size:11px;font-family:Menlo"><br></p>I am not dead set on the • operator as cartesian product and only added it back in to demonstrate that this can be  *this* pretty.<br><br><br>TJ</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Dec 17, 2015 at 7:41 PM, Al Skipp 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><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><span class=""><div><blockquote type="cite"><div>On 17 Dec 2015, at 20:25, Liam Butler-Lawrence via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div><br><div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">Comprehensions are great in Python. However, Swift can already do all of these things via for...in, map() and/or generate().</div></div></blockquote><br></div></span><div>Whether Comprehensions should be introduced to the language is worth consideration (Are they vital? Probably not). But I don’t find the Swift examples using ‘for&#39; statements particularly compelling. The ‘for’ statement needs to declare a mutable variable which is updated from within the loop. It works, but it’s not particularly elegant.</div><div><br></div><div><div style="margin:0px;font-size:11px;font-family:Menlo"><span style="color:#bb2ca2">var</span> l2: [(<span style="color:#703daa">Int</span>, <span style="color:#703daa">Int</span>)] = []</div><span class=""><div style="margin:0px;font-size:11px;font-family:Menlo"><span style="color:#bb2ca2">for</span> x <span style="color:#bb2ca2">in</span> <span style="color:#272ad8">1</span>...<span style="color:#272ad8">10</span> {</div><div style="margin:0px;font-size:11px;font-family:Menlo">  <span style="color:#bb2ca2">for</span> y <span style="color:#bb2ca2">in</span> <span style="color:#272ad8">1</span>...<span style="color:#272ad8">10</span> <span style="color:#bb2ca2">where</span> (x + y) &lt; <span style="color:#272ad8">8</span> {</div><div style="margin:0px;font-size:11px;font-family:Menlo">    <span style="color:#4f8187">l2</span>.<span style="color:#3d1d81">append</span>((x, y))</div><div style="margin:0px;font-size:11px;font-family:Menlo">  }</div><div style="margin:0px;font-size:11px;font-family:Menlo">}</div><div style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><br></div></span><div style="margin:0px;min-height:13px">It could be achieved using an expression, but it’s pretty horrible too for several reasons:</div><div style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><span style="font-family:Helvetica;font-size:12px"><br></span></div><div style="margin:0px;font-size:11px;font-family:Menlo"><span style="color:#bb2ca2">let</span> l3 = (<span style="color:#272ad8">1</span>...<span style="color:#272ad8">10</span>).flatMap { x <span style="color:#bb2ca2">in</span></div><div style="margin:0px;font-size:11px;font-family:Menlo">  (<span style="color:#272ad8">1</span>...<span style="color:#272ad8">10</span>).map { y <span style="color:#bb2ca2">in</span> (x,y) }</div><div style="margin:0px;font-size:11px;font-family:Menlo">}.filter { (x, y) <span style="color:#bb2ca2">in</span> x + y &lt; <span style="color:#272ad8">8</span> }</div><div style="margin:0px;font-size:11px;font-family:Menlo"><span style="font-family:Helvetica;font-size:12px"><br></span></div><div style="margin:0px;font-size:11px;font-family:Menlo"><span style="font-family:Helvetica;font-size:12px">(Maybe there’s a more elegant solution?)</span></div><div style="margin:0px;font-size:11px;font-family:Menlo"><span style="font-family:Helvetica;font-size:12px"><br></span></div><div style="margin:0px">If Swift had Comprehensions, they probably wouldn’t look exactly like the example below, but in my opinion it’s superior to the 2 code samples above.</div><span class=""><div style="margin:0px"><br></div><div style="margin:0px;font-size:11px;font-family:Menlo"><span style="font-family:Helvetica;font-size:12px">l2 = [(x,y) for x in range(10) for y in range(10) if x + y &lt; 8]</span><br style="font-family:Helvetica;font-size:12px"></div><div style="margin:0px;font-size:11px;font-family:Menlo"><span style="font-family:Helvetica;font-size:12px"><br></span></div></span><div style="margin:0px;font-size:11px;font-family:Menlo"><span style="font-family:Helvetica;font-size:12px">Al</span></div></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=3FGfocPbgxkNkmje7djckg9Iw-2BGYY3X6RxJ1tkUXKCqYUlttFCHU-2FIldhrv6cp5H1Zj-2BPZ2b9VdEsTuu2HT2rl4K5oANOhH32jvlBP1ChOldd-2FcgaDY412uOC0PFasbpzGYa8laT-2F6CJ0Chj75Rpjwy-2B-2F-2FnUruZgrti5QNIb38KSVLWMI6eGYfmxKeJKpKqzK6DCL0Tv7Fnd99y-2BRfTUtfG1B22Q9gWaOew-2BK2cWErY-3D" alt="" width="1" height="1" border="0" style="min-height:1px!important;width:1px!important;border-width:0!important;margin-top:0!important;margin-bottom:0!important;margin-right:0!important;margin-left:0!important;padding-top:0!important;padding-bottom:0!important;padding-right:0!important;padding-left:0!important">
</div>
<br>_______________________________________________<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/mailman/listinfo/swift-evolution</a><br>
<br></blockquote></div><br></div>