In light of the recent discussions 
(https://groups.google.com/forum/#!topic/julia-users/xJ7GpKAa16E 
and https://groups.google.com/forum/#!topic/julia-users/_lIVpV0e_WI) I got 
curious, whether there is a non-modifying version of push!, since 
push!(copy(a),b) doesn't feel right (I try to avoid modifying functions, 
except if I really need performance) and [a; b] is not type stable:

f(a) = [a; 3]
f2(a) = push!(copy(a),3)


julia> Base.return_types(f,(Array{Int64,1},))
1-element Array{Any,1}:
 Array{T,N}


julia> Base.return_types(f2,(Array{Int64,1},))
1-element Array{Any,1}:
 Array{Int64,1}


I couldn't find anything in the Julia docs. Of course, I could just define 
my own

push(a,vars...) = push!(copy(a),vars...)


but if there is a standard way to do that, I'd prefer that. Maybe there's 
also some clever way to avoid making a copy if `a` is a literal (e.g. 
push([1,2])
).

The same applies to unshift, etc.

(Non-modifying `shift!` and `pop!` already exist with the names `first` and 
`last`. I'd find it easier to remember `shift` and `pop` -- it would also 
be more similar to `merge` vs `merge!` for dictionaries, but I guess that's 
just my taste.)

Reply via email to