[swift-evolution] higher kinded types vs Python's syntactic sugars
Dave Abrahams
dabrahams at apple.com
Fri Dec 18 19:04:26 CST 2015
> On Dec 18, 2015, at 4:35 AM, Al Skipp via swift-evolution <swift-evolution at swift.org> wrote:
>
>> On 18 Dec 2015, at 00:19, T.J. Usiyan via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151218/c81381e3/attachment.html>
More information about the swift-evolution
mailing list