Travis Griggs
Travis Griggs

Reputation: 22252

How to zip two arrays of paired tuples?

I have thrown many darts at trying to get this to compile. I have two collections that are created with something like:

let ab:[(Int, Int)] = someCollection.map { thing in 
    return (a, b)
}
let cd:[(Int, Int)] = someCollection.map { thing in 
    return (c, d)
}

I want to combine them and cannot find the correct syntax to give me a working compile:

let merged:[(Int, Int)] = zip(ab, cd).map { ab:(Int, Int), cd:(Int, Int) in
    return ab[1] > cd[1] ? ab : cd
}

The parameter list for the closure is the problem. I have tried ((a, b), (c, d)) in ... and numerous other variations, with and without the :Int hints. I always get either:

Consecutive statements on a line must be separated by ';'

or

Closure tuple parameter does not support destructuring

Is there no way to zip two lists of paired tuples?

Upvotes: 1

Views: 67

Answers (1)

Rob Napier
Rob Napier

Reputation: 299345

let merged = zip(ab, cd).map { (x, y) in
    return x.1 > y.1 ? x : y
}

Upvotes: 1

Related Questions