Commit | Line | Data |
---|---|---|
967c39ef MM |
1 | module ArrayStuff where |
2 | import Data.Ix | |
3 | import Data.Array.IArray | |
4 | ||
5 | funcArray lohi f = listArray lohi $ map f $ range lohi | |
6 | ||
fd0d2377 MM |
7 | constArray lohi v = listArray lohi $ repeat v |
8 | ||
967c39ef MM |
9 | transposeArray arr = |
10 | let swap (x, y) = (y, x) in | |
11 | let (lo, hi) = bounds arr in | |
12 | ixmap (swap lo, swap hi) swap arr | |
13 | ||
14 | array2DtoListOfLists arr = | |
15 | let ((xlo, ylo), (xhi, yhi)) = bounds arr in | |
16 | map (\x -> map (\y -> arr ! (x, y)) $ range (ylo, yhi)) $ range (xlo, xhi) | |
17 | ||
18 | -- Use instead of amap when the array implementation needs to change. | |
19 | -- E.g., mapping an unboxed array to an array whose elements must be boxed. | |
20 | amap2 f arr = funcArray (bounds arr) (\i -> f (arr ! i)) | |
eb6c3c9f MM |
21 | |
22 | -- Like amap2 but the mapping function is also passed the index. | |
23 | aixmap f arr = funcArray (bounds arr) (\i -> f i (arr ! i)) |