ZipMap method extension on a collection/sequence

I was working on a project earlier this month and I needed to use elements from another array to update an existing array. Tried using the zip() function then mapping the sequence, but it would drop the rest of the elements in the array. This map function I'm pitching would retain the order and also the elements of array that the mapping closure was not applied to.

Hopefully the example below, makes it clear.

 let fooArray: [Foo] = [Foo(),Foo(),Foo(),Foo(),Foo()]
 let barArray:[Bar] = [Bar(),Bar(),Bar()]
        
//mappedFoo.count == 5
 let mappedFoo = fooArray.zipMap(against: barArray) { (foo, bar) -> Foo in
            var fooBar = foo
            fooBar.bar = bar
            return fooBar
        } 

I have a working implementation and would like to hear your thoughts.

There have been related pitches and discussions before. Perhaps you first look into these…


2 Likes
Terms of Service

Privacy Policy

Cookie Policy