I suspect there is some one-liner (or at least very simple) way of doing this. I understand how to write a function, perhaps called KronLikeMap , that does this with 2 loops. But I can’t find anything useful in the manual.
The problem may be that I don’t know what searches to try.
reduce(vcat, vector_of_arrays) is fast because it has the manual performance overload:
Since it is easy to know the output element type in this case, I’d use mapfoldl + append!. It’s faster and more robust (i.e., works even if the input is empty):
There’s probably no need to micro-optimize this. But, it is helpful to use the information of the output vector size. BangBang.collector is a composable tool for encoding this.:
julia> using BangBang
julia> @btime finish!(append!!(collector(Vector{Int}(undef, 2000), Val(true)), (y for x in 1:1000 for y in (x, 2x))));
839.000 ns (1 allocation: 15.75 KiB)