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

Frederick Kellison-Linn fred.kl at me.com
Wed Dec 9 15:33:33 CST 2015


Agreed. My original response, which did not get reply all’ed:

To me, this notation seems slightly confusing. The current syntax with an inner loop makes the time complexity apparent, and more accurately represents what you are trying to do (in this example, build the Cartesian product of suits and ranks).

The "for x in suits, y in ranks" notation hides the inner loop(s) and is not immediately apparent in its function (does this construct increment both x and y every iteration?). I definitely have seen the type of construct you are proposing many times, so there is value in considering a more concise way of writing it, but perhaps there is better notation. 

FKL

> On Dec 9, 2015, at 4:31 PM, J. Cheyo Jimenez via swift-evolution <swift-evolution at swift.org> wrote:
> 
> The proposed extension looks to me like is iterating both the suits and rank at the same time. In that case I think is a great idea.
> 
> var cards: [(Suit,Rank)] = []
> for x in suits, y in ranks {
>   cards.append((x,y))
> }
> 
> What I am expecting:
> 
> x1, y1
> x2, y2
> x3, y3
> etc
> 
> This reminds me that this is one of the ways I use the C-style for-loop. 
> The alternative now:
> 
> for (index, x) in suits.enumerate(){
>    let y = ranks[index]
>    cards.append((x,y))
> }
> 
> 
> 
> 
> 
> 
> 
> On Wed, Dec 9, 2015 at 12:00 PM, Chris Eidhof via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
> 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..<end) {
>    // 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
> 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/be2c657c/attachment.html>


More information about the swift-evolution mailing list