Read a value from the immutable array at the given index using an
applicative. This allows us to be strict in the array while remaining
lazy in the read element which is very useful for collective
operations. Suppose we want to copy an array. We could do something
like this:
copy marr arr ... = do ...
writeArray marr i (indexArray arr i) ...
...
But since the arrays are lazy, the calls to
indexArray will not
be evaluated. Rather,
marr will be filled with thunks each of
which would retain a reference to
arr. This is definitely not
what we want!
With
indexArrayM, we can instead write
copy marr arr ... = do ...
x <- indexArrayM arr i
writeArray marr i x
...
Now, indexing is executed immediately although the returned element is
still not evaluated.
Note: this function does not do bounds checking.