> On Dec 18, 2015, at 4:35 AM, Al Skipp via swift-evolution 
> <[email protected]> wrote:
> 
>> On 18 Dec 2015, at 00:19, T.J. Usiyan via swift-evolution 
>> <[email protected] <mailto:[email protected]>> wrote:
>> 
>> With a Cartesian Product type [like 
>> this](https://github.com/griotspeak/CartesianProduct 
>> <https://github.com/griotspeak/CartesianProduct>), the for-in-where syntax 
>> actually gets us to list comprehensions. I'll admit that I might not have 
>> implemented the best Cartesian Product type possible, but it should 
>> illustrate that we have what we need.
>> 
>> `for case … in cartProd(cartProd(seq1, seq2), seq3) // An operator for 
>> cartProd would make it more pleasing to read.`
> 
> That’s impressive work, but it strikes me as quite a difficult undertaking to 
> get there. (Is it just me, or are generators and sequences the most scary 
> part of Swift?) Also, is it possible to get it working as an expression, or 
> is it restricted to a ‘for’ statement? If it can only be performed as a ‘for’ 
> statement it will still need an external mutable var to be updated outside of 
> the loop. It’s fine if you want to just do side-effecty things, like print 
> the elements, but I’d consider the ability to return a value to be more 
> important.

This is a much simpler cartesian product implementation:

   seq1.flatMap { x in seq2.map { (x,$0) } }

or, if you want speed,

   seq1.lazy.flatMap { x in seq2.lazy.map { (x,$0) } }

HTH,

-Dave



_______________________________________________
swift-evolution mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to