-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix.elm
210 lines (154 loc) · 4.59 KB
/
Matrix.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
module Matrix ( Matrix
, repeat
, get
, set
, toIndexedList
, map
, mapAt
, mapAtMany
, mapAtMany'
, getMany
, setMany
, diff
) where
import Array exposing (Array)
type alias Matrix a = Array (Array a)
repeat : Int -> Int -> a -> Matrix a
repeat w h e = Array.repeat h (Array.repeat w e)
get : Int -> Int -> Matrix a -> Maybe a
get x y a =
let
row = Array.get x a
in
case row of
Nothing -> Nothing
Just row -> Array.get y row
set : Int -> Int -> a -> Matrix a -> Matrix a
set x y e a =
let
row = Array.get x a
in
case row of
Nothing -> a
Just row -> Array.set x (Array.set y e row) a
update : Int -> Int -> (a -> a) -> Matrix a -> Matrix a
update x y f a =
let
value = get x y a
in
case value of
Nothing -> a
Just e -> set x y (f e) a
-- given list of indexes, return list of objects in corresponding cells
getMany : List (Int, Int) -> Matrix a -> List a
getMany is a = List.filterMap identity
<| List.foldl (\(x,y) acc -> get x y a::acc) [] is
-- given list of indexes and list of objects,
-- update multiple cells of an array
setMany : List (Int, Int) -> List a -> Matrix a -> Matrix a
setMany is os a =
let
step ((x,y),o) acc = set x y o acc -- stupid foldl arguments
in
List.foldl step a (List.map2 (,) is os)
toIndexedList : Matrix a -> List (Int, Int, a)
toIndexedList = flatten << indexedMap (\x y e -> (y,x,e))
map : (a -> b) -> Matrix a -> Matrix b
map f a = Array.map (Array.map f) a
indexedMap : (Int -> Int -> a -> b) -> Matrix a -> Matrix b
indexedMap f a =
Array.indexedMap (\y row -> Array.indexedMap (\x e -> f x y e) row) a
-- apply function at index
mapAt : (a -> a) -> Int -> Int -> Matrix a -> Matrix a
mapAt f x y a =
let
e = get x y a
in
case e of
Nothing -> a
Just e -> set x y (f e) a
mapAtMany : (a -> a) -> List (Int, Int) -> Matrix a -> Matrix a
mapAtMany f is a =
let
step (x,y) acc =
case get x y a of
Nothing -> acc
Just e -> set x y (f e) acc
in
List.foldl step a is
-- Takes list of indexes and list of objects;
-- combines old object under index with new object using function.
mapAtMany' : (a -> b -> a) -> List (Int, Int) -> List b -> Matrix a -> Matrix a
mapAtMany' f is os a =
let
step ((x,y),o) acc =
case get x y a of
Nothing -> acc
Just e -> set x y (f e o) acc
in
List.foldl step a (List.map2 (,) is os)
-- remove all elements from both lists that don't satisfy the predicate,
-- then apply the function
filterMap2 : (a -> b -> Bool) -> (a -> b -> c) -> List a -> List b -> List c
filterMap2 p f xs ys = List.map (uncurry f)
<| List.filter (uncurry p) (List.map2 (,) xs ys)
-- return indexes of cells that are not equal
diff : Matrix a -> Matrix a -> List (Int, Int)
diff a1 a2 =
let
il1 = toIndexedList a1
il2 = toIndexedList a2
filteredList = filterMap2 (/=)
(\(x,y,e) _ -> (x,y))
il1 il2
in
filteredList
flatten : Matrix a -> List a
flatten = List.concat << toList
flattenArray : Matrix a -> Array a
flattenArray = Array.foldl Array.append Array.empty
toList : Matrix a -> List (List a)
toList = List.map Array.toList << Array.toList
getRow : Int -> Matrix a -> Maybe (Array a)
getRow n a = Array.get n a
getCol : Int -> Matrix a -> Maybe (Array a)
getCol n a = maybeSequenceArray <| Array.map (Array.get n) a
width : Matrix a -> Int
width a =
let
row = Array.get 0 a
in
case row of
Nothing -> 0
Just row -> Array.length row
height : Matrix a -> Int
height a = Array.length a
size : Matrix a -> (Int, Int)
size a = (width a, height a)
-- sequence on Maybe monad
-- a helper function
maybeSequence : List (Maybe a) -> Maybe (List a)
maybeSequence = listTraverse identity
listTraverse : (a -> Maybe b) -> List a -> Maybe (List b)
listTraverse f =
let
step e acc =
case f e of
Nothing -> Nothing
Just x -> Maybe.map ((::)x) acc
in
List.foldr step (Just [])
listAp : List (a -> b) -> List a -> List b
listAp fs xs =
List.concatMap (\x -> List.map (\f -> f x) fs) xs
maybeSequenceArray : Array (Maybe a) -> Maybe (Array a)
maybeSequenceArray = arrayTraverse identity
arrayTraverse : (a -> Maybe b) -> Array a -> Maybe (Array b)
arrayTraverse f =
let
step e acc =
case f e of
Nothing -> Nothing
Just x -> Maybe.map (Array.push x) acc
in
Array.foldl step (Just Array.empty)