Adding any two numberic tuples in swift

后端 未结 2 1551
旧时难觅i
旧时难觅i 2021-02-06 08:20

I have created one function to add tuples which looks like (Int, Int).

func + (x: (T, T), y: (T, T)) -> (T, T) {
    return (x.0 + y.0, x.1         


        
2条回答
  •  庸人自扰
    2021-02-06 08:43

    You can use the Array solution as suggested by @Cristik or you can also make use of closure returning variadic function like:

    func add(_ a: T...) -> (_ b: T...) -> [T] {
        return { (b: T...) -> [T] in
            return zip(a, b).map { $0.0 + $0.1 }
        }
    }
    
    let sum = add(1, 2,3)(4, 5, 6)
    
    print(sum)
    

提交回复
热议问题