I often try to write mutating functions to avoid unnecessary allocation. But sometimes I have to use the same function and allocate a new array. This usually means that I have to create a new function with almost the same content.
A = randn(3)
function foo!(A)
A .= A ./ 2
return nothing
end
function foo()
A = randn(3)
A .= A ./ 2
return A
end
Is there a good practise to avoid copy/paste of mutating functions into normal functions? Would this example is considered a right approach?
thanks! what if I have multiple function arguments, for which I also would like to specify types?
C=0.5
function foo(C::Float64)
A = randn(3)
A .= A ./ 2 .+ C
return A
end
A = randn(3)
function foo!(A,C::Float64)
A .= A ./ 2 .+ C
return nothing
end
foo2(C::Float64) = foo!(randn(3),C::Float64)