Hello! I would like to write a generic function that, given a generic vector of tuples of equal but variable length, such as (for length three):
mytuple = [(1, "a", "α"), (2, "b", "β"), (3, "c", "γ")]
returns me separate vectors corresponding to each position, as follows.
# Write a function used to destructure a vector of tuples into separate, argument-specific vectors
function unzip(tuple)
return map(collect, zip((Tuple(e) for e ∈ tuple)...))
end
julia> unzip(mytuple)
3-element Vector{Vector}:
[1, 2, 3]
["a", "b", "c"]
["α", "β", "γ"]
I have, however, struggled with declaring the appropriate type in the function argument. Suppose I try:
function unzip_new(tuple::Vector{Tuple{Vararg{Any}}})
return map(collect, zip((Tuple(e) for e ∈ tuple)...))
end
function unzip_new(tuple::Vector{Tuple{Any}})
return map(collect, zip((Tuple(e) for e ∈ tuple)...))
end
then, any run of unzip_new(mytuple)
returns me an error of the kind “no method matching type”. How is it the case? I tried several variations of type declaration in the function argument, but to no avail.
To give you guys a bit of context, the reason why I need to do this, is that I want to perform a function that returns multiple values (in a tuple) in a loop. Hence, I would ask the loop or a suitable comprehension to give me the vector of tuples corresponding to the collection of results in the loop, and then “reshape” the results in order to obtain the proper vectors. Not sure if this is the right approach though.