# Optimal way to flatten array

I have an array of array

``````y = [ones(Float32, 3) for i=1:3]
sum(abs2, y)
``````

which I can’t apply `abs2` to.
I need it to compute my loss function

Now I use custom function

``````function flat_this(x)
arr = ones(0)
for i=1:length(x)
arr = vcat(arr, x[i])
end
return arr
end
``````

But I doubt it’s optimal and fast enough

`reduce(vcat, y)`

or in this case, to avoid an intermediary Array

`sum(i -> sum(abs2,i), y)`

3 Likes

Or another alternative:
`sum(abs2,Iterators.flatten(y))`

3 Likes

`Iterators.flatten` doesn’t allocate because it’s lazy but it’s slower (I have no idea why)

``````julia> y = [rand(100) for i in 1:100];

julia> @btime sum(abs2, reduce(vcat, \$y))
7.200 μs (2 allocations: 78.20 KiB)
3333.509067117525

julia> @btime sum(i -> sum(abs2,i), \$y)
1.400 μs (0 allocations: 0 bytes)
3333.5090671175253

julia> @btime sum(abs2,Iterators.flatten(\$y))
9.600 μs (0 allocations: 0 bytes)
3333.5090671175276
``````
3 Likes

Another way to flatten `y`, quite inefficient compared to `reduce(vcat,y)` above, though:

``````[yi for yr in y for yi in yr]
``````
1 Like

Thanks for replies