For many optimisation methods the parameters must be given as a single vector, such as Optim.jl. But for many interesting functions the parameters/coeffients to be optimised are matrices, or a mixture of matrices and vectors. Often this mix can be put into a composite type.I got sick of rewriting methods for doing this foreach occurrence, so I made some general ones, including using staged functions to produce them for composite types.function pack{T}(sources::AbstractArray{T}...)    total_length = sum(map(length,sources))    package = Vector{T}(total_length)    @inbounds pack!(package, sources...)    packageendfunction pack!{T}(output::Vector{T}, sources::AbstractArray{T}...)    ii = 1    for s in sources        output[ii:ii+length(s)-1]=vec(s)        ii+=length(s)    end    outputendfunction unpack!{T}(package::Vector{T}, dests::AbstractArray{T}...)    ii = 1    for s in dests        s[:]= package[ii:ii+length(s)-1]        ii+=length(s)    end    destsendfunction unpack{T}(package::Vector{T}, sizes::Union{Int,Tuple{Vararg{Int}}}...)    ds = [Array{T}(sz) for sz in sizes]    @inbounds unpack!(package, ds...)    dsend@generated function pack(x)    Expr(:call, pack, [:(x.$fn) for fn in fieldnames(x)]...)end@generated function pack!(dest, x)    Expr(:call, pack!, [:dest, [:(x.$fn) for fn in fieldnames(x)]...]...)end@generated function unpack!(package, x)    Expr(:block,        Expr(:call, unpack!, [:package, [:(x.$fn) for fn in fieldnames(x)]...]...),        :(x)    )endI was cautious with my use of inbounds to only have it where I knew the vectors were the right size since I was creating them.In C, I could have accomplished this by simply unioning the struct of arrays with a array. But I can't work out how to do that in Julia, even when going down into pointer stuff. I don't think even immutable composite types are guaranteed bits-type if they have array members.
Flattening lists of Vectors and Matrices into arrays
vectors;julia