Append to a CartesianIndex

Viewed 39

How do I append to a CartesianIndex? Example:

index = CartesianIndex(3,3)
append!(index, 1)
# Desired output -> CartesianIndex(3, 3, 1)

I can do this in a roundabout way converting the index to a tuple, adding the 1, and then converting back. However, is there a better way?

1 Answers

CartesianIndexes are immutable, so you can't modify! them in-place. Instead, just create a new one based on the existing one:

julia> index = CartesianIndex(3,3)
CartesianIndex(3, 3)

julia> CartesianIndex(index, 1)
CartesianIndex(3, 3, 1)
Related