Concatenating 2D generator of arrays

Hi, I’m trying to concatenate an 2D generator that generate an 2D array of 2x2 arrays, such as this one
iter = (rand(2,2) for i in 1:d1, j in 1:d2 )
I would need to concatenate these arrays into a 2 by d1 by 2 by d2 array, ideally w/o first generating an array of wrong permutation and then permute. I tried cat(iter..., dims=(1,3)) or cat(iter..., dims=(2,4)), neither worked.

I also read a little about this thread, however, I don’t want to collect(iter) before the concatenation or use LazyArrays as the latter is probably not as efficient when later used for LA operations. In other words, I want the memory to be allocated exactly as specified dimensions.

So what is the right way to generate this concatenation correctly?

Thank you!

If you can afford collecting your iter generator:

julia> using SplitApplyCombine

julia> combinedims([rand(2,2) for i in 1:3, j in 1:5], (2, 4))
2×3×2×5 Array{Float64, 4}:
...

If you need to keep the generator lazy and only materialize the final array:

julia> using SplitApplyCombine, GeneratorArrays

julia> combinedims(array(rand(2,2) for i in 1:3, j in 1:5), (2, 4))
2×3×2×5 Array{Float64, 4}:
...

Alternatively, see mapview() in SplitApplyCombine.jl for lazy arrays.

Note that the first - eager - approach can be faster than the lazy one.