<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 <<a href="mailto:a.michail@me.com" class="">a.michail@me.com</a>> 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 <<a href="mailto:ccruden@novafore.com" class="">ccruden@novafore.com</a>> 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=""> h <- host</div><div class=""> p <- 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 <<a href="mailto:tseitz42@icloud.com" class="">tseitz42@icloud.com</a>> 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() -> Future<Int> { … }</div></div><div class=""><div class="">func someOtherAsyncCall() -> Future<Int> { … }</div></div><div class=""><br class=""></div><div class="">// mixing Scala and Swift syntax here...</div><div class="">let z: Future<Int> = for {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>x <- someAsyncCall()</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>y <- 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: <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 <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>>:</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. </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. </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..<m { for col in 0..<n where row+col < 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=""> row <- 0 to m </div><div class=""> col <- 0 to n</div><div class=""> if row+col < 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 => (0 to n).withFilter(y => x + y < 5).map(y => (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 <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> 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 <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> 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..<m { for col in 0..<n { yield (row,col) } }<br class=""><br class="">let c:[(Int,Int)] = for row in 0..<m { for col in 0..<n where row+col < 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"> </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>