# Anyway to deal with tuple dynamically? Re: Algorithm.Product2Sequence elements are nested tuple, how to support multiple product terms dynamically?

To calculate product of character sequence using Algorithms' `product(_:_:)` for multiple terms, it returns nested `Product2Sequence`. I don't know how to use `product(_:_:)` to calculate variable number of product terms. For now I can only do terms of 1 to 11:

``````    // two terms
static func digitsToLetters(_ first: Int, _ second: Int) -> [String] {
let product = product(first.letters, second.letters)
return product.map { String([\$0, \$1]) }
}

// three terms:
static func digitsToLetters(_ first: Int, _ second: Int, _ third: Int) -> [String] {
let product = product(product(first.letters, second.letters), third.letters)
return product.map({ t, e in
String([t.0, t.1, e])
})
}

// four terms
static func digitsToLetters(_ first: Int, _ second: Int, _ third: Int, _ forth: Int) -> [String] {
let product = product(product(product(first.letters, second.letters), third.letters), forth.letters)
return product.map { t, e in
let ((p3, p2), p1) = t
return String([p3, p2, p1, e])
}
}

// so on to 11 ...

// eleven terms
static func digitsToLetters(_ first: Int, _ second: Int, _ third: Int, _ forth: Int, _ fifth: Int, _ sixth: Int, _ seventh: Int, _ eighth: Int, _ ninth: Int, _ tenth: Int, _ eleventh: Int) -> [String] {
let product = product(product(product(product(product(product(product(product(product(product(first.letters, second.letters), third.letters), forth.letters), fifth.letters), sixth.letters), seventh.letters), eighth.letters), ninth.letters), tenth.letters), eleventh.letters)
return product.map { t, e in            // <== map here is very slow
let (((((((((p10, p9), p8), p7), p6), p5), p4), p3), p2), p1) = t
return String([p10, p9, p8, p7, p6, p5, p4, p3, p2, p1, e])
}
}
``````

Is there anyway to use `product(_:_:)` to dynamically handle any number of product terms? Something like this:

``````func digitsToLetters(_ digits: Int...) -> [String] {
// let's assume we have 2 or more digits
// calculate product of n digit letters
// the result of this is recursively generic of Product2Sequence<Product2Sequence<...>>>
let product = product(  ... )     // er how to calculate product of any number of terms?
// assume we can get the `product` calculated, now it's `Product2Sequence` which can contain tuples
// how to now dynamically access the value in those tuples?
return product.map { tuple, element in
// dynamically access into the nested tuple
// ??
}
}
``````

Edit: `Algorithms.product(_:_:)` is this way now:

``````public func product<Base1: Sequence, Base2: Collection>(
_ s1: Base1, _ s2: Base2
) -> Product2Sequence<Base1, Base2> {
Product2Sequence(s1, s2)
}
``````

Maybe if we have variatic generic parameters, this function can handle two or more variable number of product collections term? But even if this is solved, the elements in the result are in tuples, which can only be handle statically at compile time?