I’m playing around with Julia 1.0.3 and getting confused. I have written a function that mutates an array. I want to use this function to mutate a part of a larger array. I can’t wrap my head around why some things work and some don’t. Here are examples:
function f!(x)
for i in eachindex(x)
x[i] = i
end
end
x = randn(2, 3)
f!(view(x, :, :))
x
If x is an array of Any, your mutating function works as is
> x = Array{Any}(undef,2,3)
> f!(view(x,1:2,1:2))
> x
2×3 Array{Any,2}:
CartesianIndex(1, 1) CartesianIndex(1, 2) #undef
CartesianIndex(2, 1) CartesianIndex(2, 2) #undef
You can explicitly ask for linear (integer) indexes using eachindex(IndexLinear(),x) (these would probably be a bit less efficient for arbitrary views, since the iteration logic is more complicated).