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

Colin Barrett colin at springsandstruts.com
Wed Dec 9 16:38:27 CST 2015


Indeed. In fact, ilya posted this already with an implementation upthread :) 

BTW, the mathematical term is “Cartesian product”, named so because this operation is how you construct the famous Cartesian xy-coordinate system we all learned in Algebra class.

FWIW I agree that this should be a library function and not in the language.

-Colin

> On Dec 9, 2015, at 5:31 PM, David Waite via swift-evolution <swift-evolution at swift.org> wrote:
> 
> You could have a function (lets call it cross for lack of me knowing a better mathematical term) that takes in multiple CollectionTypes and returns a CollectionType of all the combinations of the constituent sequence type elements.
> 
> for (x,y) in cross(0..<width, 0..<height) { … }
> 
> My swift-fu however is not yet strong enough for me to know how to avoid the backing generators being CrossGenerator2, CrossGenerator3, etc.
> 
> -DW
> 
>> On Dec 9, 2015, at 3:11 PM, Chris Eidhof via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> 
>> Exactly! Just like multiple-if-let is the same as flatMap on optionals, this is a better syntax for flatMap on arrays. 
>> 
>> To make it more clear that it’s a nested loop, we could also consider:
>> 
>> for x in xs, for y in ys {
>> }
>> 
>> But I’m not sure if it actually is clearer. (You could totally still interpret this as a zip, rather than a flatMap).
>> 
>> Chris
>> 
>> 
>> 
>>> On 09 Dec 2015, at 16:51, Maxwell Swadling <maxs at apple.com <mailto:maxs at apple.com>> wrote:
>>> 
>>> 
>>>> On 9 Dec 2015, at 1:44 PM, Douglas Gregor via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>>> 
>>>>> 
>>>>> On Dec 9, 2015, at 1:43 PM, Brent Royal-Gordon <brent at architechies.com <mailto:brent at architechies.com>> 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))
>>>>>>> }
>>>>>> 
>>>>>> 
>>>>>> Isn’t this just
>>>>>> 
>>>>>> 	for (x, y) in zip2(suits, ranks) {
>>>>>> 	}
>>>>>> 
>>>>>> ?
>>>>> 
>>>>> No, apparently it’s supposed to be all permutations of both types. But I too read it as syntactic sugar for a zip, which I think is a great reason not to add this syntax.
>>>> 
>>>> 
>>>> Ah. In that case, I’d much rather have a library function that indicates that we’re getting all permutations. This doesn’t feel like it belongs in the language at all, but in the library.
>>>> 
>>>> 	- Doug
>>>> 
>>>> _______________________________________________
>>>> 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>
>>> 
>>> We do have this library function, it is flatMap.
>>> 
>>> The desired feature here seems to be desugaring for loops  
>>> 
>>> for x in xs, y in ys, z in zs {
>>>   ...
>>> }
>>> 
>>> into:
>>> 
>>> xs.flatMap { x in ys.flatMap { y in zs.map { z in ... } } }
>>> 
>>> So as above you would get:
>>> 
>>> suits.flatMap { x in ranks.map { y in cards.append((x,y)) } }
>>> 
>>> 
>> 
>>  _______________________________________________
>> 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/7fc1a57a/attachment.html>


More information about the swift-evolution mailing list