<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="">I don’t know - the syntax you gave me won’t run in the playground…..<div class=""><br class=""></div><div class="">the for-comprehension aka “list comprehension” (or in this case optional comprehension) is from Scala.</div><div class=""><br class=""></div><div class="">I was wondering if there was an easy way to state the same with optionals in Swift.</div><div class=""><br class=""></div><div class="">I hesitate to paste in my solution since there is likely a more reasonable way to chain optionals that would effectively do the same as for-comprehension of optionals in Scala (different language, different ways of approaching things).</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On 2016-01-19, at 21:20:21, Amir Michail &lt;<a href="mailto:a.michail@me.com" class="">a.michail@me.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Jan 19, 2016, at 7:26 AM, Craig Cruden &lt;<a href="mailto:ccruden@novafore.com" class="">ccruden@novafore.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><br class=""></div><div class="">What is the most readable/concise (balance) for writing something like the following in swift:</div><div class=""><br class=""></div><div class=""><div class="">val host: Option[String] = Some("host")</div><div class="">val port: Option[Int] = Some(80)</div><div class="">val addr = for {</div><div class="">&nbsp; h &lt;- host</div><div class="">&nbsp; p &lt;- port</div><div class="">} yield new InetSocketAddress(h, p)</div></div></div></div></blockquote><div class=""><br class=""></div><div class="">Wouldn’t “let/val { ...” be better than “for { ...” in this context?</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><div class=""><br class=""></div><div class="">i.e. If either of the optionals is None then the result will be None otherwise it will be Some(inetSocketAddress).</div><div class=""><br class=""></div></div><br class=""><div class=""><blockquote type="cite" class=""><div class="">On 2016-01-15, at 5:57:33, Thorsten Seitz &lt;<a href="mailto:tseitz42@icloud.com" class="">tseitz42@icloud.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Scala’s for-comprehensions are very powerful, precisely because they are using flatMap etc.<div class="">This allows using them with other types instead of collections, e.g. asynchronous calls:</div><div class=""><br class=""></div><div class=""><div class="">func someAsyncCall() -&gt; Future&lt;Int&gt; { … }</div></div><div class=""><div class="">func&nbsp;someOtherAsyncCall() -&gt; Future&lt;Int&gt; { … }</div></div><div class=""><br class=""></div><div class="">// &nbsp;mixing Scala and Swift syntax here...</div><div class="">let z: Future&lt;Int&gt; = for {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>x &lt;- someAsyncCall()</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>y &lt;- someOtherAsyncCall()</div><div class="">} yield (x + y)</div><div class=""><br class=""></div><div class=""><div class="">The important thing for this to look nice is that there is no nesting like in your Swift example. Otherwise you get a pyramid of doom (think of more async calls).</div><div class=""><br class=""></div><div class="">Basically it is the same as Haskell’s do notation and you can do powerful things with that like the async example. Another nice example is the abstraction of Bayes’ rules presented in this blog:&nbsp;<a href="http://www.randomhacks.net/2007/02/22/bayes-rule-and-drug-tests/" class="">http://www.randomhacks.net/2007/02/22/bayes-rule-and-drug-tests/</a></div><div class=""><br class=""></div><div class="">-Thorsten</div><div class=""><br class=""></div><div class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">Am 14.01.2016 um 22:10 schrieb Craig Cruden via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt;:</div><br class="Apple-interchange-newline"><div class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: 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; float: none; display: inline !important;" class="">I believe this is the same as Scala’s for-comprehension….. and Scala’s for-comprehension is actually just syntactic sugar for a combination of flatMap, map and withFilter.</span><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: 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;"><br class=""></div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: 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;">Although harder to read for many… it likely could be written similarly in Swift. &nbsp;</div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: 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;"><br class=""></div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: 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;">i.e.&nbsp;</div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: 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;"><br class=""><blockquote type="cite" class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class=""><blockquote type="cite" class=""><div class=""><div class="">for row in 0..&lt;m { for col in 0..&lt;n where row+col &lt; 5 { yield (row,col) } }</div></div></blockquote></div></div></blockquote><div class=""><br class=""></div><div class="">in Scala is</div><div class=""><br class=""></div><div class="">for {</div><div class="">&nbsp; &nbsp;row &lt;- 0 to m&nbsp;</div><div class="">&nbsp; &nbsp;col &lt;- 0 to n</div><div class="">&nbsp; &nbsp;if row+col &lt; 5</div><div class="">} yield (row, col)</div><div class=""><br class=""></div><div class="">which is actually (after the compiler is done with it):</div><div class=""><br class=""></div><div class="">(0 to m).flatMap(x =&gt; (0 to n).withFilter(y =&gt; x + y &lt; 5).map(y =&gt; (x, y)))</div><div class=""><br class=""></div><div class="">I have not tried to do it in the playground but I would expect since I believe all the functions are available in Swift.</div><div class=""><br class=""></div><div class="">Note: 0 to m // is a range in Scala</div><div class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>withFilter is a filter which does not copy the contents of what it is filtering…. it is just applying the filter as needed when mapping.</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On 2016-01-15, at 3:35:23, Félix Cloutier 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 class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">This has been proposed before and Chris said that it would be out of scope for Swift 3. There is definitely interest in this area though.<br class=""><div class=""><br class="Apple-interchange-newline"><span class="" style="font-family: 'Lucida Grande'; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;">Félix</span></div><br class=""><div class=""><blockquote type="cite" class=""><div class="">Le 14 janv. 2016 à 15:30:07, Amir Michail via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; a écrit :</div><br class="Apple-interchange-newline"><div class=""><div class="">These would be like the unrolled versions of Python’s generator expressions.<br class=""><br class="">Examples:<br class=""><br class="">let a:[Int] = for x in l { yield x*2 }<br class=""><br class="">let b:[(Int,Int)] = for row in 0..&lt;m { for col in 0..&lt;n { yield (row,col) } }<br class=""><br class="">let c:[(Int,Int)] = for row in 0..&lt;m { for col in 0..&lt;n where row+col &lt; 5 { yield (row,col) } }<br class=""><br 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" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></div></blockquote></div><br class=""><img src="https://u2002410.ct.sendgrid.net/wf/open?upn=CGU22LnxbYa2EM3wKvzuC6syQDwKa0tMs5IyT5gL1wJtVJjrHLMpZ8eCa6gc-2BmuCrCCWV1dnMWQXeaD9yuBN328bT1BNYtsQVeugjG-2BoW4TnIKcKkJgl6b2g71UfswrNjawbKl2Ea-2FDNp45okBQMZ8e7fuD67YOmoc1MG76Njm0hyYjl7UU-2F5BBoWaS-2FIgqEFkuU8D5b-2FxxTejPCsDy9yMiSH2PxC7hFkBvjXaYkuWA-3D" alt="" width="1" height="1" border="0" class="" style="height: 1px !important; width: 1px !important; border-width: 0px !important; margin: 0px !important; padding: 0px !important;"></div>_______________________________________________<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" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class=""></div></div><img src="https://u2002410.ct.sendgrid.net/wf/open?upn=1MXK54sosN3xru3iYcLt0oBZ2w20i49gyogXctgrspdLtzDa2bLWwFC0tJtQ7LGhGb46Mu1HLr-2FkkUKZgWQL8VE-2Fflt001jZjBOGxmOCb9QCISq-2BvUSEvgQthX35DhA23QLbSf-2F5U6Ycf63FH5V-2Bc5IgWYlxGm352d16VeJ3L3BUokXbEDmBl-2BjHLhoorqGW058aGs0wwHs1y0MNK8FY-2B73GTP2-2BA-2FXes25vJRQtnyI-3D" alt="" width="1" height="1" border="0" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: 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; height: 1px !important; width: 1px !important; border-width: 0px !important; margin: 0px !important; padding: 0px !important;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: 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; float: none; display: inline !important;" class=""><span class="Apple-converted-space">&nbsp;</span>_______________________________________________</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: 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=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: 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; float: none; display: inline !important;" class="">swift-evolution mailing list</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: 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=""><a href="mailto:swift-evolution@swift.org" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: 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="">swift-evolution@swift.org</a><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: 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=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: 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="">https://lists.swift.org/mailman/listinfo/swift-evolution</a></div></blockquote></div><br class=""></div></div></div></div></blockquote></div><br class=""></div></div></blockquote></div><br class=""></div></div></blockquote></div><br class=""></div></body></html>