Looping over each index in varargs

I have a function that takes in any number of vectors, not necessarily of equal length:

function exfnc(x::Vector{Float64}...)
    sizes = length.(x)
    ## compute some function over all combinations (x1[i], x2[j], ..., xn[k])?
end

Say there are n such vectors and I have some function that takes in this n-vector. I want to compute this function over all possible combinations of the elements between each vector. For example, if I only had to consider three arguments, I could write something like:

function exfnc(x₁, x₂, x₃)
    for k in eachindex(x₃)
        for j in eachindex(x₂)
            for i in eachindex(x₁)
                val = f(x₁[i], x₂[j], x₃[k]) # some function f
                # do something with val...
            end
        end
    end
end

Is there a way to form this type of loop for the Varargs case?

Perhaps you want Iterators.product?

function exfnc(x...)
    for p in Iterators.product(x...)
        val = f(p...) # some function f
        # do something with val...
    end
end
2 Likes