No collect!() function?

seems like an implementation of collect!() is missing?

how could I do in-place collection like collect!(v, range(1, 10, length=3)) then?

Thanks.

append!

Also, copyto! can be used for this

julia> copyto!(zeros(10), 1:10)
10-element Array{Float64,1}:
  1.0
  2.0
  3.0
  4.0
  5.0
  6.0
  7.0
  8.0
  9.0
 10.0
4 Likes

yes! copyto!() works (but not append!() that should sometimes allocate).

julia> v = randn(5);
julia> copyto!(v, range(1, 10, length = 5))
5-element Array{Float64,1}:
  1.0
  3.25
  5.5
  7.75
 10.0

julia> v
5-element Array{Float64,1}:
  1.0
  3.25
  5.5
  7.75
 10.0

julia> @btime copyto!($v, $range(1, 10, length = 5));
  122.753 ns (0 allocations: 0 bytes)

so, is it a confusion in function naming? why not rename copyto!() as collect!()?

The confusion is on your part. collect will of course allocate a new vector, that’s its job. append! will have to allocate, as it appends to an existing one (but it is pretty smart about it). copyto! copies to an existing container, which you have to preallocate, with a known element type — very differently from collect, which figures out these things for you.

1 Like

I’d use broadcasted assignment here — simply v .= range(1, 10, length=3).

9 Likes

Understand now. Thanks.