Imagine I want to map a function over an array, but the function has a type not just of
a -> b
but
a -> Int -> b
i.e. the function also takes an index. How do I do that?
Imagine I want to map a function over an array, but the function has
Share
Short answer, use
traverse.Longer example:
arr1is a 2×3 matrice.traverseis a function that takes (1) the original array, (2) a function for mapping source indices to target indices, and (3) a function that is given (i) a lookup function into the original array and (ii) an index that returns a new value.So here
arr2modifies each of the original elements by adding the row and column indices of that particular entry.