In F# array2Ds are mutable which doesn’t make for the most functional code. At the moment, I’m hiding this by doing a Array2D.copy of my inputs when I want to make a change and return the array. i.e.
let test (x : int[,]) =
let y = Array2D.copy x
y.[0,0] <- 3
Is there a better approach that makes test more functional?
You can always use high-order functions on Array2D module to work with
Array2Din a functional style. Particularly, Array2D.mapi operating on array indices is suitable for this task:EDIT:
Generally speaking, your
testfunction is fine since you can limit side effects inside the function only. You need to be more careful in the case of nested arrays; however, writing your own functions with controlled side effects usually gives better performance than using high-order functions forArray2D.