<div dir="ltr"><div>You can define a 'times' operation to work with any sequences, e.g.</div><div><br></div><div><div>import Foundation</div><div><br></div><div>enum Suits: String {</div><div> case Spades = "♠"</div><div> case Hearts = "♥"</div><div> case Diamonds = "♦"</div><div> case Clubs = "♣"</div><div>}</div><div><br></div><div>let suits:[Suits] = [.Spades, .Hearts, .Diamonds, .Clubs]</div><div>let ranks = ["A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3", "2"]</div><div><br></div><div>infix operator ⨉ {}</div><div><br></div><div>func ⨉<A:SequenceType, B:SequenceType>(lhs: A, rhs: B)</div><div> -> [(A.Generator.Element, B.Generator.Element)] {</div><div> </div><div> return lhs</div><div> .map{ left in rhs.map{ right in (left, right) }}</div><div> .reduce([], combine: +)</div><div>}</div><div><br></div><div>[1, 2] ⨉ [3, 4]</div><div><br></div><div>func shuffled() -> [String] {</div><div> var cards:[String] = []</div><div> </div><div> for (suit, rank) in suits ⨉ ranks {</div><div> let random = Int(abs(rand())) % (cards.count + 1)</div><div> cards.insert(suit.rawValue + rank, atIndex: random)</div><div> }</div><div> </div><div> return cards</div><div>}</div><div><br></div><div>shuffled().joinWithSeparator(" ")</div></div><div><br></div><div><br></div><div><br></div><br><div class="gmail_quote"><div dir="ltr">On Wed, Dec 9, 2015 at 23:00 Chris Eidhof via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">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:<br>
<br>
var cards: [(Suit,Rank)] = []<br>
for x in suits, y in ranks {<br>
cards.append((x,y))<br>
}<br>
<br>
This would be the same as writing:<br>
<br>
var cards: [(Suit,Rank)] = []<br>
for x in suits {<br>
for y in ranks {<br>
cards.append((x,y))}<br>
}<br>
}<br>
<br>
You could also do something like:<br>
<br>
for x in input1, y in (x..<end) {<br>
// Do something with (x,y)<br>
}<br>
<br>
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...<br>
<br>
Chris<br>
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</blockquote></div></div>