Module

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

#enumerate Source

enumerate :: forall a. Grid a -> Array (Tuple Coordinates a)

Return the elements of the grid in a flat array, within a tuple containing their original 2D coordinates.

#differencesFrom Source

differencesFrom :: forall a. Eq a => Grid a -> Grid a -> Array (Tuple Coordinates a)

Return an array of elements and their coordinates where the two supplied grids differ. If the grids have different dimensions, return all of the elements from the first grid.

#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

#replicate Source

replicate :: forall a. Int -> Int -> a -> Grid a

Fill a grid of size width x height with the value a. The value is the same in all of the cells in the grid.

#Coordinates Source

type Coordinates = { x :: Int, y :: Int }

A helper type representing a point on a grid.

Instances

#fromFoldable Source

fromFoldable :: forall f a. Foldable f => Int -> Int -> f a -> Maybe (Grid a)

Create a Grid of the given width and height from any Foldable, like arrays and lists. If the number of elements in array isn't exactly equal to width * height — in other words, there are too many or too few elements — return Nothing.&.

#construct Source

construct :: forall a. Int -> Int -> (Coordinates -> a) -> Grid a

Create a grid of size width x height filled with values returned by a (Coordinates -> a) function. The funcion gets called on every coordinate in the grid and the value it returns is used to fill that specific position. The smallest coordinate is { x: 0, y: 0 } in the upper-left corner of the grid.

It is possible to implement replicate with the help of construct:

replicate w h value = Grid.construct w h (\_ -> value)

For example, Grid.construct 3 3 (\{x, y} -> (x - y) == 0) would create the following grid

true  false false
false true  false
false false true

#constructM Source

constructM :: forall m a. Monad m => Int -> Int -> (Coordinates -> m a) -> m (Grid a)