/
Grid.elm
237 lines (194 loc) · 6.5 KB
/
Grid.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
module Grid exposing (FromRowsAndColumnsProblem, Grid, fromRowsAndColumns, fromRowsAndColumnsArray, get, getWrapping, indexedMap, initialize, map, rotate, set, toArrays, topLeft, update, view, windows)
import Array exposing (Array)
import Color.Transparent as Color exposing (Color)
import Css
import Html.Styled as Html exposing (Html)
import Html.Styled.Attributes as Attrs exposing (css, style)
import Set
type Grid a
= Grid
{ -- rows x columns
items : Array a
, width : Int
, height : Int
}
type FromRowsAndColumnsProblem
= MoreThanOneWidth (List Int)
initialize : { rows : Int, columns : Int } -> ({ row : Int, column : Int } -> a) -> Grid a
initialize { rows, columns } init =
Grid
{ items =
Array.initialize (rows * columns)
(\i ->
init
{ row = i // rows
, column = modBy rows i
}
)
, width = columns
, height = rows
}
{-| Construct a grid from a list of lists. The outer list is a list of rows,
and the inner lists are values in columns.
If the sizes of the column arrays (the inner ones) don't match up, you'll get a
`MoreThanOneWidth` error back from this function.
-}
fromRowsAndColumns : List (List a) -> Grid a
fromRowsAndColumns rowsAndColumns =
Array.fromList (List.map Array.fromList rowsAndColumns)
|> fromRowsAndColumnsArray
fromRowsAndColumnsArray : Array (Array a) -> Grid a
fromRowsAndColumnsArray rowsAndColumns =
Grid
{ items = Array.foldr Array.append Array.empty rowsAndColumns
, width =
Array.get 0 rowsAndColumns
|> Maybe.map Array.length
|> Maybe.withDefault 0
, height = Array.length rowsAndColumns
}
toArrays : Grid a -> Array (Array a)
toArrays (Grid grid) =
Array.initialize grid.height
(\row ->
Array.slice
(row * grid.width)
(row * grid.width + grid.width)
grid.items
)
{-| Rotate a grid 90° clockwise.
-}
rotate : Grid a -> Grid a
rotate ((Grid { width, height }) as grid) =
let
newItems =
List.range 0 (width - 1)
|> List.map
(\col ->
column col grid
|> Maybe.withDefault Array.empty
|> Array.foldr Array.push Array.empty
)
|> List.foldr Array.append Array.empty
in
Grid
{ items = newItems
, height = width
, width = height
}
column : Int -> Grid a -> Maybe (Array a)
column colNum (Grid { items, height, width }) =
List.range 0 (height - 1)
|> List.foldl
(\row soFar ->
Maybe.andThen
(\arr ->
Array.get (row * width + colNum) items
|> Maybe.map (\val -> Array.push val arr)
)
soFar
)
(Just Array.empty)
{-| Get a number of windows over the given grid data. Windows wrap around the
edges of the input grid. We include tuples here, as they're useful as IDs.
-}
windows : { width : Int, height : Int } -> Grid a -> Grid (Grid a)
windows sizes ((Grid { width, height, items }) as grid) =
Grid
{ items =
Array.initialize (height * width)
(\cell ->
cropWrapping
{ row = cell // width
, column = modBy width cell
, width = sizes.width
, height = sizes.height
}
grid
)
, width = width
, height = height
}
cropWrapping : { row : Int, column : Int, width : Int, height : Int } -> Grid a -> Grid a
cropWrapping bounds (Grid grid) =
let
expanded =
toArrays (Grid grid)
|> Array.map (\row -> Array.append row row)
|> (\all -> Array.append all all)
wrappedRow =
modBy grid.height bounds.row
wrappedColumn =
modBy grid.width bounds.column
in
Grid
{ items =
expanded
|> Array.slice wrappedRow (wrappedRow + bounds.height)
|> Array.map (Array.slice wrappedColumn (wrappedColumn + bounds.width))
|> Array.foldr Array.append Array.empty
, width = bounds.width
, height = bounds.height
}
get : { row : Int, column : Int } -> Grid a -> Maybe a
get coords (Grid { items, width }) =
if coords.row < 0 || coords.column < 0 then
Nothing
else
Array.get (coords.row * width + coords.column) items
{-| Still a maybe because the grid could be empty
-}
getWrapping : { row : Int, column : Int } -> Grid a -> Maybe a
getWrapping coords ((Grid { width, height }) as grid) =
get
{ row = modBy height coords.row
, column = modBy width coords.column
}
grid
topLeft : Grid a -> Maybe a
topLeft =
get { row = 0, column = 0 }
set : { row : Int, column : Int } -> a -> Grid a -> Grid a
set coords newValue (Grid grid) =
Grid
{ grid
| items =
Array.set
(coords.row * grid.width + coords.column)
newValue
grid.items
}
update : (a -> a) -> { row : Int, column : Int } -> Grid a -> Grid a
update fn coords grid =
case get coords grid of
Just item ->
set coords (fn item) grid
Nothing ->
grid
indexedMap : ({ row : Int, column : Int } -> a -> b) -> Grid a -> Grid b
indexedMap fn (Grid grid) =
Grid
{ items =
Array.indexedMap
(\i val ->
fn
{ row = modBy grid.width i
, column = remainderBy grid.width i
}
val
)
grid.items
, width = grid.width
, height = grid.height
}
map : (a -> b) -> Grid a -> Grid b
map fn =
indexedMap (\_ a -> fn a)
{-| TODO: could probably do this with CSS grids but I'm not sure how.
-}
view : (a -> Html msg) -> Grid a -> Html msg
view viewItem grid =
toArrays grid
|> Array.map (Array.map viewItem >> Array.toList >> Html.tr [])
|> Array.toList
|> Html.table [ css [ Css.borderCollapse Css.collapse ] ]