Missing `append!` method?

#1

I just tried to do the following:

append!([1, 2], [3, 4], [5, 6])

And was surprised to find that there’s no such method. I was expecting it to be the equivalent of:

append!(append!([1, 2], [3, 4]), [5, 6])

Is this just a missing method that could be added, or is there a reason this isn’t supported?

0 Likes

#2

You are probably looking for vcat.

vcat([1, 2], [3, 4], [5, 6])

On the other hand if you are looking to append multiple vectors for intance

a=[1,2]
b=[[3,4],[5,6]]
append!(a,Iterators.flatten(b))
a==1:6 #true
3 Likes

#3

A general approach for case like this is foldl (or reduce): foldl(append!, [[3, 4], [5, 6]], init=[1, 2])

You can use it when you have a function that “aggregates” two inputs.

2 Likes