<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=""><div><blockquote type="cite" class=""><div class="">On 17 Dec 2015, at 20:25, Liam Butler-Lawrence via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">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 class=""></div><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' 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 class=""></div><div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> l2: [(<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int</span>)] = []</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">for</span> x <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">in</span> <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span>...<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">10</span> {</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">&nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">for</span> y <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">in</span> <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span>...<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">10</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">where</span> (x + y) &lt; <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">8</span> {</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">l2</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">append</span>((x, y))</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">&nbsp; }</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">}</div><div style="margin: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; min-height: 13px;" class="">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;" class=""><span style="font-family: Helvetica; font-size: 12px;" class=""><br class=""></span></div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span> l3 = (<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span>...<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">10</span>).flatMap { x <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">in</span></div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">&nbsp; (<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span>...<span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">10</span>).map { y <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">in</span> (x,y) }</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">}.filter { (x, y) <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">in</span> x + y &lt; <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">8</span> }</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""><span style="font-family: Helvetica; font-size: 12px;" class=""><br class=""></span></div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""><span style="font-family: Helvetica; font-size: 12px;" class="">(Maybe there’s a more elegant solution?)</span></div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""><span style="font-family: Helvetica; font-size: 12px;" class=""><br class=""></span></div><div style="margin: 0px;" class="">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><div style="margin: 0px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""><span style="font-family: Helvetica; font-size: 12px;" class="">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;" class=""></div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""><span style="font-family: Helvetica; font-size: 12px;" class=""><br class=""></span></div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""><span style="font-family: Helvetica; font-size: 12px;" class="">Al</span></div></div></body></html>