[swift-evolution] Extending the for loop to have multiple clauses

Chris Eidhof chris at eidhof.nl
Wed Dec 9 15:13:20 CST 2015


Yes, it’d be great if you can combine it with where. Alternatively, it could be interesting to instead explore array comprehension syntax, rather than making `for` smarter. That’d allow for a more functional style. For example, Norvig’s spelling corrector (http://norvig.com/spell-correct.html) would then be very straightforward to port.

Re the x operator: it’s even easier if you define it with flatMap. However, the for syntax is different, because the second clause can depend on the first...

Chris

> On 09 Dec 2015, at 15:48, Jacob Bandes-Storch <jtbandes at gmail.com> wrote:
> 
> Are you expecting that this could be combined with "where" clauses?
> 
> for x in range1 where x%2==0, y in range2 where y%3==0 {}
> 
> for x in range1, y in range2 where x%2==0 && y%3==0 {}
> 
> I think it's a good idea, but the implications may be less than obvious.
> On Wed, Dec 9, 2015 at 12:37 PM krzysztof at siejkowski.net <mailto:krzysztof at siejkowski.net> via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
> +1. Way more readable than nested loops and consistent with already existing syntax.
> 
> 
> -----Original Message-----
> From: Chris Eidhof via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>>
> Reply: Chris Eidhof <chris at eidhof.nl <mailto:chris at eidhof.nl>>
> Date: December 9, 2015 at 9:00:26 PM
> To: swift-evolution at swift.org <mailto:swift-evolution at swift.org> <swift-evolution at swift.org <mailto:swift-evolution at swift.org>>
> Subject:  [swift-evolution] Extending the for loop to have multiple clauses
> 
> > I think it could be really nice to extend the for-loop so that it can have multiple clauses.
> > Much like in the if-let with multiple clauses, I could imagine a for-loop with multiple
> > clauses:
> >
> > var cards: [(Suit,Rank)] = []
> > for x in suits, y in ranks {
> > cards.append((x,y))
> > }
> >
> > This would be the same as writing:
> >
> > var cards: [(Suit,Rank)] = []
> > for x in suits {
> > for y in ranks {
> > cards.append((x,y))}
> > }
> > }
> >
> > You could also do something like:
> >
> > for x in input1, y in (x..> // Do something with (x,y)
> > }
> >
> > In fact, once we would have that, we could combine both if-let and for, and make it more
> > general, to end up with something like Haskell’s do-notation or C#’s LINQ. But that might
> > be taking it too far...
> >
> > Chris
> > _______________________________________________
> > swift-evolution mailing list
> > swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> > https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
> >
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151209/42c66b4a/attachment.html>


More information about the swift-evolution mailing list