TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

List Comprehension in Swift

56 pointsby DaNmarnerover 7 years ago

4 comments

59nadirover 7 years ago
I really dislike the proposed way to do this. If this is &quot;Swifty&quot;, give me something else.<p>The Haskell list comprehension made sense to me the first time I saw it, but there is no way I&#x27;d know what the mess in the article was doing until someone explained to me.<p>Design has to come first. You can&#x27;t just go with what&#x27;s &quot;Swifty&quot; if that doesn&#x27;t convey what&#x27;s happening in a reasonable fashion.
评论 #15891303 未加载
评论 #15891243 未加载
Someoneover 7 years ago
<i>List</i> comprehension is a bad idea, IMO. They were an improvement over not having anything like it, but python has improved over it (<a href="https:&#x2F;&#x2F;wiki.python.org&#x2F;moin&#x2F;Generators" rel="nofollow">https:&#x2F;&#x2F;wiki.python.org&#x2F;moin&#x2F;Generators</a>)<p>⇒ <i>Generator expressions</i> are the way to go. They would give you the sequence of elements without generating the (potentially enormous) data structure.<p>From there, methods to reify the items in a sequence would give you your list, array, or dictionary, where needed.<p>So, for Swift, I wouldn’t use<p><pre><code> Array(1..&lt;5, 3..&lt;5) { ($0, $1) } </code></pre> but the slightly longer<p><pre><code> Array(for i in 1..&lt;5, j in 3..&lt;5 yield (i,j)) </code></pre> I’m not sure that is easy to fit in the existing parser, though. If it can be fit in, I would allow that code in ‘normal’ nested for loops, too.
评论 #15892387 未加载
thewayfarerover 7 years ago
&gt; (Can’t wait until we can have variadic generic parameters!)<p>This.
评论 #15890594 未加载
评论 #15891839 未加载
ralfdover 7 years ago
Can someone explain the n modulo operation to me?
评论 #15891152 未加载