A Grid is a dictionary that has a size constraint.
Here is an example where such a grid is used:
Snake Example.
Create a grid
dimensions : { columns:Int , rows:Int }
dimensions =
{ columns=42
, rows=3
}
grid : Grid a
grid =
empty dimensions
fill (always <| Just ()) dimensions |> empty(Int,Int)s
--> []Create an empty grid
dimensions : { columns:Int , rows:Int }
dimensions =
{ columns=42
, rows=3
}
empty dimensions
--> fill (always Nothing ) dimensionsInsert a value at a (Int,Int) in a grid. Replaces value when there is a collision.
dimensions : { columns:Int , rows:Int }
dimensions =
{ columns=42
, rows=3
}
grid : Grid a
grid =
empty dimensions
grid |> insert (2,2) 42 |> get (2,2)
--> Just 42Update the value of a grid for a specific (Int,Int) with a given function.
dimensions : { columns:Int , rows:Int }
dimensions =
{ columns=42
, rows=3
}
grid : Grid a
grid =
empty dimensions
grid |> update (2,2) (always <| Just 42)
--> grid |> insert (2,2) 42Remove a vlaue from a grid. If the (Int,Int) is empty, no changes are made.
dimensions : { columns:Int , rows:Int }
dimensions =
{ columns=42
, rows=3
}
grid : Grid a
grid =
empty dimensions
grid |> insert (2,2) 42 |> get (2,2) --> Just 42
grid |> insert (2,2) 42 |> remove (2,2) |> get (2,2)
--> NothingDetermine if a grid is empty.
dimensions : { columns:Int , rows:Int }
dimensions =
{ columns=42
, rows=3
}
grid : Grid a
grid =
empty dimensions
grid |> isEmpty --> True
grid |> insert (2,2) 42 |> isEmpty --> FalseDetermine if a (Int,Int) is empty.
dimensions : { columns:Int , rows:Int }
dimensions =
{ columns=42
, rows=3
}
grid : Grid a
grid =
empty dimensions
grid |> insert (2,2) 42 |> member (2,2)
--> TrueGet the value associated with a (Int,Int). If the (Int,Int) is empty, return Nothing.
dimensions : { columns:Int , rows:Int }
dimensions =
{ columns=42
, rows=3
}
grid : Grid a
grid =
empty dimensions
grid |> insert (2,2) 42 |> get (2,2)
--> Just 42Determine the number of values in the grid.
dimensions : { columns:Int , rows:Int }
dimensions =
{ columns=42
, rows=3
}
grid : Grid a
grid =
empty dimensions
grid |> insert (2,2) 42 |> size
--> 1Return the dimensions of the grid.
dim : { columns:Int , rows:Int }
dim =
{ columns=42
, rows=3
}
grid : Grid a
grid =
empty dim
grid |> dimensions
--> dimGet all non empty positions in a grid, sorted from lowest to highest.
dimensions : { columns:Int , rows:Int }
dimensions =
{ columns=42
, rows=3
}
grid : Grid a
grid =
empty dimensions
grid |> insert (2,2) 42 |> positions
--> [(2,2)]Get all empty positions in a grid, sorted from lowest to highest.
dimensions : { columns:Int , rows:Int }
dimensions =
{ columns=42
, rows=3
}
grid : Grid Int
grid =
fill
(always <| Just <| 42)
dimensions
grid |> remove (2,2) |> emptyPositions
--> [(2,2)]Get all of the values in a grid, in the order of their (Int,Int)s.
dimensions : { columns:Int , rows:Int }
dimensions =
{ columns=42
, rows=3
}
grid : Grid a
grid =
empty dimensions
grid |> insert (2,2) 42 |> values
--> [42]Convert a grid into an association list of (Int,Int)-value pairs, sorted by the (Int,Int).
dimensions : { columns:Int , rows:Int }
dimensions =
{ columns=42
, rows=3
}
grid : Grid Int
grid =
empty dimensions
grid |> insert (2,2) 42 |> toList
--> [( (2,2), 42 )]Convert an association list into a grid.
dimensions : { columns:Int , rows:Int }
dimensions =
{ columns=42
, rows=3
}
grid : Grid Int
grid =
empty dimensions
[((2,2),42),((2,1),20)] |> fromList dimensions
--> grid |> insert (2,2) 42 |> insert (2,1) 20Convert a grid into an associated dictionary
dimensions : { columns:Int , rows:Int }
dimensions =
{ columns=42
, rows=3
}
grid : Grid Int
grid =
empty dimensions
|> insert (2,2) 42
grid |> toDict |> fromDict dimensions |> get (2,2)
--> Just 42Convert an dictionary to a grid
dimensions : { columns:Int , rows:Int }
dimensions =
{ columns=42
, rows=3
}
grid : Grid Int
grid =
empty dimensions
grid |> toDict |> fromDict dimensions |> get (2,2)
--> NothingApply a function to all (Int,Int)s in a grid.
dimensions : { columns:Int , rows:Int }
dimensions =
{ columns=42
, rows=3
}
empty dimensions |> map (\_ _ -> Just 42)
--> fill (always <| Just 42) dimensionsFold over all (Int,Int)s in a grid, row by row, from top down.
Fold over all (Int,Int)s in a grid, row by row, from bottum up.
Keep only the values that pass the given test.
dimensions : { columns:Int , rows:Int }
dimensions =
{ columns=42
, rows=3
}
grid : Grid Int
grid =
empty dimensions
|> insert (2,4) 2
|> insert (2,3) 42
grid |> filter (\_ -> (==) 42) |> values
--> [42]Partition a grid according to some test.
The first grid contains all values which passed the test, and the second contains the values that did not.
Find the first value that passes a given test.
Combine two grids. If there is a collision, preference is given to the first grid.
Keep a value when its (Int,Int) appears in the second grid. Preference is given to values in the first grid.
A grid with a fixes amount of columns and rows.
It will wrap the borders (apply ModBy), making every (Int,Int) valid.
If instead you want to have hard border around your grid, use
Grid.Borderedinstead.