Add command-line interface ./match (adapted from POPL 2012 version).
[match/match.git] / program / ArrayStuff.hs
CommitLineData
967c39ef
MM
1module ArrayStuff where
2import Data.Ix
3import Data.Array.IArray
4
5funcArray lohi f = listArray lohi $ map f $ range lohi
6
fd0d2377
MM
7constArray lohi v = listArray lohi $ repeat v
8
967c39ef
MM
9transposeArray 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
14array2DtoListOfLists 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
bbabbd01
MM
18listOfListsToArray2D ll =
19 listArray ((0, 0), (length ll - 1, length (head ll) - 1)) $ concat ll
20
967c39ef
MM
21-- Use instead of amap when the array implementation needs to change.
22-- E.g., mapping an unboxed array to an array whose elements must be boxed.
23amap2 f arr = funcArray (bounds arr) (\i -> f (arr ! i))
eb6c3c9f
MM
24
25-- Like amap2 but the mapping function is also passed the index.
26aixmap f arr = funcArray (bounds arr) (\i -> f i (arr ! i))