Data.Grid
- Package
- purescript-grid-reactors
- Repository
- Eugleo/purescript-grid-reactors
This module supplies a simple datastructure for working with 2D grids.
Grid
is based on (immutable) arrays from the module Data.Array
, so it has
similar performance characteristics: fast access (anywhere), slow updates
(the whole structure is copied), efficient memory usage.
#Grid Source
data Grid a
A grid is represented a plain 1D array. It is saved there row by row, starting from the top. Functions in this module take 2D coordinates, but translate them interally to 1D index into the array.
Constructors
Instances
#differencesFrom Source
differencesFrom :: forall a. Eq a => Grid a -> Grid a -> Array (Tuple Coordinates a)
#updateAt Source
updateAt :: forall a. Coordinates -> a -> Grid a -> Maybe (Grid a)
Change the element at the specified index, creating a new grid, or returning Nothing
if the index is out of bounds.
#modifyAt Source
modifyAt :: forall a. Coordinates -> (a -> a) -> Grid a -> Maybe (Grid a)
Apply a function to the element at the specified index, creating a new grid,
or returning Nothing
if the index is out of bounds.
#updateAt' Source
updateAt' :: forall a. Coordinates -> a -> Grid a -> Grid a
Change the element at the specified index, creating a new grid, or don't do anything when the index is out of bounds.
#modifyAt' Source
modifyAt' :: forall a. Coordinates -> (a -> a) -> Grid a -> Grid a
Change the element at the specified index, creating a new grid, or don't do anything when the index is out of bounds.
#index Source
index :: forall a. Grid a -> Coordinates -> Maybe a
This function provides a safe way to read a value at a particular index from a grid.
The position { x: 0, y: 0 }
is in the top left corner of the grid.
sentence = Grid.fromFoldable 2 2 [ "Hello", "World", "Guys", "!" ]
index sentence { x:0, y:0 } = Just "Hello"
index sentence { x:2, y:0 } = Nothing