13

I just discovered the Swift zip function recently. It seems quite useful.

It takes 2 input arrays and creates an array of tuples out of pairs of values from each array.

Is there a variant of zip that takes an arbitrary number of arrays and outputs tuples with that same number of elements? It seems like there should be a way to do this.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • I'm no expert at Swift but while you can create a function that takes a variable number of sequence parameters, how would you declare the return type to be a tuple of unknown size? – rmaddy Nov 09 '16 at 23:45
  • Good point. You can use generics to define the function to create tuples who's elements are the types of the input array elements, but I don't know of a way in Swift to define a tuple of variable size. – Duncan C Nov 09 '16 at 23:49

4 Answers4

19

Bear in mind, you can nest one zip inside another, and then unpack it with a nested tuple:

let integers = [1, 2, 3, 4, 5]
let strings = ["a", "b", "c", "d", "e"]
let doubles = [1.0, 2.0, 3.0, 4.0, 5.0]

for (integer, (string, double)) in zip(integers, zip(strings, doubles)) {
    print("\(integer) \(string) \(double)")
}

Not quite as elegant as having a zip for arbitrary n-tuples, but it gets the job done.

Airspeed Velocity
  • 40,491
  • 8
  • 113
  • 118
12

No, zip for an arbitrary number of sequences isn't currently possible due to Swift's lack of variadic generics. This is discussed in the Generics Manifesto.

In the meanwhile, I wrote a gyb template for generating ZipSequences of custom arity. I've also pre-generated ZipSequences of arity 3...10 for your convenience. It's available here.

In action:

let integers = [1, 2, 3, 4, 5]
let strings = ["a", "b", "c", "d", "e"]
let doubles = [1.0, 2.0, 3.0, 4.0, 5.0]

for (integer, string, double) in zip(integers, strings, doubles) {
    print("\(integer) \(string) \(double)")
}

Prints:

1 a 1.0

2 b 2.0

3 c 3.0

4 d 4.0

5 e 5.0

Community
  • 1
  • 1
Alexander
  • 59,041
  • 12
  • 98
  • 151
0

You can use the nested zip approach as in some of the previous answers, and use map to flatten the result:

let combined = zip(integers, zip(strings, doubles)).map { ( $0.0, $0.1.0, $0.1.1 ) }

It yields an array of tuples, each with three elements. Looks a bit ugly but as long as it works… It does work as long as the number of arrays to zip is fixed.

Tom E
  • 1,530
  • 9
  • 17
0

If you accept the output as an array instead of a tuple then here is a solution. This is utilizing the heterogeneous array of type [Any] in Swift 5.7

func zipAny(_ data: [[Any]])->[[Any]]{
    let count = data.first!.count
    let initArray = Array(repeating: [], count: count)
    let reduced : [[Any]] = data.reduce(initArray, {
        Array(zip($0,$1)).map({
            var arrayCopied : [Any] = $0.0
            arrayCopied.append( $0.1 )
            return arrayCopied
        })
    })
    return reduced
}

let inputArray = [[1,2,3],["a","b","c"]]
let outputArray = zipAny(inputArray)

print(outputArray)
// [[1, "a"], [2, "b"], [3, "c"]]
luomein
  • 51
  • 1
  • 4