[swift-evolution] higher kinded types vs Python's syntactic sugars
T.J. Usiyan
griotspeak at gmail.com
Fri Dec 18 20:33:30 CST 2015
I was just talking about this kind of thing (I've updated the repo with up
to 5-tuples) for times where you seq1 • seq2 • seq3
// 'Cartesian product' of a Cartesian product and a collection. Meant to
help manage type explosion.
public func •<Left: SequenceType, Right: SequenceType, A, B where
Left.Generator.Element == (A, B)>(lhs:Left, rhs:Right) -> [(A, B, Right.
Generator.Element)] {
return lhs.flatMap { x in rhs.map { (x.0, x.1, $0) } }
}
On Fri, Dec 18, 2015 at 9:25 PM, Dave Abrahams <dabrahams at apple.com> wrote:
>
> On Dec 18, 2015, at 5:46 PM, T.J. Usiyan <griotspeak at gmail.com> wrote:
>
> Updated the library with that. The only downside that I can see is that
> flattening, as I had always planned but finally bothered to do, has no way
> to tell if the left tuple was the produce of a previous cartesian product
> operation.
>
>
> Not sure what that means, I’m afraid.
>
> Small price to pay and all that. Thanks for the insight!
>
>
> Glad to help.
>
> On Fri, Dec 18, 2015 at 8:19 PM, T.J. Usiyan <griotspeak at gmail.com> wrote:
>
>> That is… damned nice. I choose not to feel that bad about it… for reasons.
>>
>>
>>
>> On Fri, Dec 18, 2015 at 8:04 PM, Dave Abrahams <dabrahams at apple.com>
>> wrote:
>>
>>>
>>> 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> wrote:
>>>
>>> With a Cartesian Product type [like this](
>>> 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
>>>
>>>
>>>
>>>
>>
>
> -Dave
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151218/167c11b8/attachment.html>
More information about the swift-evolution
mailing list