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
init (always <| Just ()) dimensions |> emptyPositions
--> []Create an empty grid
dimensions : { columns:Int , rows:Int }
dimensions =
{ columns=42
, rows=3
}
empty dimensions
--> init (always Nothing ) dimensionsBuild a grid out of lists
Generate a random grid based on a cell generator
Get 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 (Maybe a)
grid =
empty dimensions
grid |> insert (2,2) 42 |> getMember (2,2)
--> Just 42Insert 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 (Maybe a)
grid =
empty dimensions
grid |> insert (2,2) 42 |> getMember (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 (Maybe 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 (Maybe a)
grid =
empty dimensions
grid |> insert (2,2) 42 |> getMember (2,2) --> Just 42
grid |> insert (2,2) 42 |> remove (2,2) |> getMember (2,2)
--> NothingDetermine if a grid is empty.
dimensions : { columns:Int , rows:Int }
dimensions =
{ columns=42
, rows=3
}
grid : Grid (Maybe 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 (Maybe a)
grid =
empty dimensions
grid |> insert (2,2) 42 |> member (2,2)
--> TrueGet the element in the grid
Set the element in the grid
Determine the number of values in the grid.
dimensions : { columns:Int , rows:Int }
dimensions =
{ columns=42
, rows=3
}
grid : Grid (Maybe 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 (Maybe 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 (Maybe 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 (Maybe Int)
grid =
init
(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 (Maybe 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=2
, rows=2
}
grid : Grid (Maybe Int)
grid =
empty dimensions
grid |> insert (1,1) 42 |> toList
--> [( (0,0), Nothing ),((1,0), Nothing ),((0,1), Nothing ),( (1,1), Just 42 )]Convert an association list into a grid.
dimensions : { columns:Int , rows:Int }
dimensions =
{ columns=42
, rows=3
}
grid : Grid (Maybe 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 (Maybe Int)
grid =
empty dimensions
|> insert (2,2) 42
grid |> toDict |> fromDict dimensions |> getMember (2,2)
--> Just 42Convert an dictionary to a grid
dimensions : { columns:Int , rows:Int }
dimensions =
{ columns=42
, rows=3
}
grid : Grid (Maybe Int)
grid =
empty dimensions
grid |> toDict |> fromDict dimensions |> getMember (2,2)
--> NothingApply a function to all positions in a grid.
dimensions : { columns:Int , rows:Int }
dimensions =
{ columns=42
, rows=3
}
empty dimensions |> map (\_ _ -> Just 42)
--> init (always <| Just 42) dimensionsKeep only the values that pass the given test.
dimensions : { columns:Int , rows:Int }
dimensions =
{ columns=42
, rows=3
}
grid : Grid (Maybe Int)
grid =
empty dimensions
|> insert (2,4) 2
|> insert (2,2) 42
grid |> filter (\_ -> (==) 42) |> values
--> [42]Find the first value that passes a given test.
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.