Hi Michael,
You can actually use splice!:
julia> splice!(x, 3:2, y)
0-element Array{Int64,1}
julia> x
8-element Array{Int64,1}:
1
2
3
4
5
6
7
8
3:2 is a convention in julia that indicates the (empty) location in the
array between index 2 and index 3 (e.g., searchsorted(x,3) for your
original x will return 3:2 as the insertion point).
Note that splice returns any removed ("spliced out") elements, which is why
the return value of the first command above is empty
Hope this helps!
Cheers,
Kevin
On Tue, Jul 15, 2014 at 5:23 PM, Michael Louwrens <
[email protected]> wrote:
> Should insert! not be able to insert a collection?
>
> x = [1,2,7,8]
> y = [3,4,5,6]
> insert!(x,2+1,y)
>
> Is then unable to complete the insertion and create [1,2,3,4,5,6,7,8].
> There is a costlier way to do this at the moment however.
>
> splice! almost replicates the required functionality but it replaces the
> item at 2 instead of inserting items after position2.
>
> x = [1,2,7,8]
> y = [3,4,5,6]
> splice!(x,2,unshift!(y,x[2]))
>
> This is about 20% slower but does work.
>
> I was surprised that insert! inserts the item before the item at that
> index instead of after. Could this perhaps be mentioned in the doc for
> insert!?
> Perhaps my Google-Fu is weak, but I could not find any reference to say
> which behaviour it should have.
>
> Just wanted to point out that currently insert! only works for a single
> item instead of a single item and collections and to inquire if there is
> not perhaps a better workaround than the above.
>
> Thanks!
>