[swift-evolution] zip3, zip4, ...

Vinicius Vendramini vinivendra at gmail.com
Mon Dec 7 07:12:41 CST 2015


I wouldn't really know how to do this, but a better approach than implementing zip2 through zip10 might be making it a variadic function, no?

> On Dec 6, 2015, at 6:49 PM, Donnacha Oisín Kidney via swift-evolution <swift-evolution at swift.org> wrote:
> 
> An implementation of this is actually pretty complicated, since you aren’t supposed to call a generator once it’s returned nil.
> 
> public struct NilPaddedZipGenerator<G0: GeneratorType, G1: GeneratorType> : GeneratorType {
>   
>   private var (g0, g1): (G0?, G1?)
>   
>   public mutating func next() -> (G0.Element?, G1.Element?)? {
>     let (e0,e1) = (g0?.next(),g1?.next())
>     switch (e0,e1) {
>     case (nil,nil): return nil
>     case (  _,nil): g1 = nil
>     case (nil,  _): g0 = nil
>     default: break
>     }
>     return (e0,e1)
>   }
> }
> 
> public struct NilPaddedZip<S0: SequenceType, S1: SequenceType> : LazySequenceType {
>   
>   private let (s0, s1): (S0, S1)
>   public func generate() -> NilPaddedZipGenerator<S0.Generator, S1.Generator> {
>     return NilPaddedZipGenerator(g0: s0.generate(), g1: s1.generate())
>   }
> }
> 
> @warn_unused_result
> public func zipWithPadding<S0: SequenceType, S1: SequenceType>(s0: S0, _ s1: S1)
>   -> NilPaddedZip<S0, S1> {
>     return NilPaddedZip(s0: s0, s1: s1)
> }
> 
>>> On 6 Dec 2015, at 23:44, Erica Sadun via swift-evolution <swift-evolution at swift.org> wrote:
>>> 
>>> Is there an implementation in the stdlib for (T?, T?) like this?
>>> 
>>> On Dec 6, 2015, at 4:37 PM, Dmitri Gribenko <gribozavr at gmail.com> wrote:
>>> 
>>>> On Sun, Dec 6, 2015 at 3:34 PM, Erica Sadun via swift-evolution <swift-evolution at swift.org> wrote:
>>>> It's pretty easy to build your own Zips. Not sure the language really needs this. For example, I recently built a zip that produces (T?, T?) which fills one of the two with nil until both lists are consumed:
>>>> 
>>>> func longZip<S0: SequenceType, S1: SequenceType>(seq0: S0, _ seq1: S1) ->
>>>>     AnyGenerator<(S0.Generator.Element?, S1.Generator.Element?)> {
>>> 
>>> 
>>> Just wanted to point out that AnyGenerator has an inherent cost from the type erasure.  The implementation in the standard library uses generics and is fully optimizable.
>>> 
>>> Dmitri
>>> 
>>> -- 
>>> main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
>>> (j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/
>> 
>>  _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
> 
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151207/e6c0de3a/attachment.html>


More information about the swift-evolution mailing list