This is a situation I face very often: I have the same operation that needs to be run once for each dimension of a 3D construct.
This is complicated because the order of indices differs for each variant, and I don’t know how to express this different ordering elegantly, causing me to duplicate or triplicate lots of code.
Here’s one example: Let’s say the task is to flip two dimensions of a point, the dimension staying intact is given as an integer. This would be a solution like the ones I usually write, which are annoying me because they are so redundant and need control flow:
function flip_parts(point, dim::Int)
if dim == 1
(point[1], point[3], point[2])
elseif dim == 2
(point[3], point[2], point[1])
elseif dim == 3
(point[2], point[1], point[3])
else
error()
end
end
I don’t know how to generically express the different ordering of elements to build the return tuples given only the dim input argument.
Who can point me to an elegant way of solving problems like this? Ideally in the most generic and performant way possible?