If I understand it correctly, a function that looks like returning multiple values actually returns a Tuple of the values and therefore the result of broadcasting on the function is an array of the Tuples:
function f1(x,y,c); return x+c*y; end
function f2(x,y,c); return x+c*y, x-c*y; end
xs = 1:3
ys = 7:9
r1 = f1.(xs,ys,2)
r2 = f2.(xs,ys,2) # Vector of Tuples
s2, t2 = f2.(xs,ys,2);
s2 # the 1st Tuple
t2 # the 2nd Tuple
But, I need s2
and t2
to be vectors of returned values. What’s the idiomatic way to get them?
During this little research, I learned that columntable()
from Tables
can do that
using Tables
s2, t2 = columntable(f2.(xs,ys,2));
s2 # Vector
t2 # Vector
columntable()
seems to be originally designed for NamedTuples. It assigns some names to the elements of the Tuple and convert the Vector of the NamedTuples to a NamedTuple of Vectors. In the above sample code, the tuple deconstruction strips out the names.
But, “transposing” Vectors of Tuples or Vectors of Vectors is a common enough situation that there should be a common idiom, I imagine.