Stack arrays into array with extra dimension

This pattern comes up often for me (eg analyizing simulations, posterior predictive checks). I have a solution, but I wonder if there is a better one. MWE:

"""
Collect arrays in the argument such that

`stack1(v)[1, fill(:, ndims(v[i]))...] == v[i]`
"""
stack1(v) =  reshape(transpose(hcat(map(vec, v)...)), :, size(first(v))...)

g = (reshape((1:6)*i, 2, 3) for i in 1:10);
s = stack1(g);

so that

julia> s[1, :, :]
2×3 Array{Int64,2}:
 1  3  5
 2  4  6

julia> s[2, :, :]
2×3 Array{Int64,2}:
 2  6  10
 4  8  12

I do things like stack1(map(f, as, bs)) where as and bs are different and mapslices would not work.

julia> g = (reshape((1:6)*i, 1, 2, 3) for i in 1:10);

julia> s = cat(1, g...);

julia> s[1,:,:]
2×3 Array{Int64,2}:
 1  3  5
 2  4  6

julia> s[2,:,:]
2×3 Array{Int64,2}:
 2  6  10
 4  8  12
2 Likes