/
SlowGrid.elm
221 lines (182 loc) · 6.29 KB
/
SlowGrid.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
module SlowGrid 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 (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 (\row -> Array.initialize columns (\colNum -> init { row = row, column = colNum }))
, 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 = 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 { items }) =
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
)
|> Array.fromList
in
Grid
{ items = newItems
, height = width
, width = height
}
column : Int -> Grid a -> Maybe (Array a)
column colNum (Grid { items, height }) =
List.range 0 (height - 1)
|> List.foldl
(\row soFar ->
Maybe.andThen
(\arr ->
items
|> Array.get row
|> Maybe.andThen (Array.get colNum)
|> 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 }) =
let
-- when we reach the edge, we just need to wrap around.
-- Repeating once per axis should do it!
expanded =
Array.initialize (height * 2)
(\i ->
let
row =
items
|> Array.get (modBy height i)
|> Maybe.withDefault Array.empty
in
Array.append row row
)
in
Grid
{ items =
Array.initialize height
(\row ->
Array.initialize width
(\col ->
Grid
{ items =
expanded
|> Array.slice row (row + sizes.height)
|> Array.map (Array.slice col (col + sizes.width))
, width = sizes.width
, height = sizes.height
}
)
)
, width = width
, height = height
}
get : { row : Int, column : Int } -> Grid a -> Maybe a
get coords (Grid { items }) =
items
|> Array.get coords.row
|> Maybe.andThen (Array.get coords.column)
{-| 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) =
case Array.get coords.row grid.items of
Nothing ->
Grid grid
Just row ->
Grid
{ grid
| items =
Array.set
coords.row
(Array.set coords.column newValue row)
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
{-| 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 { items }) =
items
|> Array.map (Array.map viewItem >> Array.toList >> Html.tr [])
|> Array.toList
|> Html.table [ css [ Css.borderCollapse Css.collapse ] ]
indexedMap : ({ row : Int, column : Int } -> a -> b) -> Grid a -> Grid b
indexedMap fn (Grid grid) =
Grid
{ items =
Array.indexedMap
(\rowNum row ->
Array.indexedMap
(\colNum val -> fn { row = rowNum, column = colNum } val)
row
)
grid.items
, width = grid.width
, height = grid.height
}
map : (a -> b) -> Grid a -> Grid b
map fn =
indexedMap (\_ a -> fn a)