Orasund/pixelengine/5.0.0

NOTE: the latest version is 6.2.0

Grid.Bordered

A grid with a hard border around the edges. you cant read or write from squares outside the border. Here is an example where such a grid is used: Space Invaders.

Error

type Error

Possible Errors.

  • OutOfBounds - the position is not valid
  • NotSucessful - the function as a problem fullfilling the task. (Maybe the position is occupied?)
ignoringErrors : (Grid a -> Result result (Grid a)) -> Grid a -> Grid a

Trys modifying a grid and if its fails it returns the original grid.

Please be aware, that by using this function you might introduce errors.

Grids

type Grid a

A grid of values.

It has a fixed amount of columns and rows.

If case of a invalid position, it returns an error.

grid |> Grid.insert ( -1, 0 ) virus == Err OutOfBounds

Build

(Position -> Maybe a)
-> { rows : Int
, columns : Int
}
-> Grid a

Create a grid

{ rows : Int
, columns : Int
}
-> Grid a

Create an empty grid

insert : Position -> a -> Grid a -> Result Error (Grid a)

Insert a value at a position in a grid. Returns a NotSuccessful error if the position is occupied.

Position
-> (Maybe a -> Result () (Maybe a))
-> Grid a
-> Result Error (Grid a)

Update the value of a grid for a specific position with a given function.

remove : Position -> Grid a -> Result Error (Grid a)

Remove a vlaue from a grid. If the position is empty, it returs a NotSuccessful error.

Query

isEmpty : Grid a -> Bool

Determine if a grid is empty.

member : Position -> Grid a -> Result () Bool

Determine if a position is empty.

get : Position -> Grid a -> Result () (Maybe a)

Get the value associated with a position. If the position is empty, return Nothing.

isValid : Position -> Grid a -> Bool

checks if a position is valid.

grid : Grid a
grid =
    empty
        { columns=42
        , rows=3
        }




grid |> isValid (-1,0) --> False
grid |> isValid (41,0) --> True
grid |> isValid (42,0) --> False
size : Grid a -> Int

Determine the number of values in the grid.

Grid a
-> { columns : Int
, rows : Int
}

return the dimensions of the grid

List

positions : Grid a -> List Position

Get all non empty positions in a grid, sorted from lowest to highest.

emptyPositions : Grid a -> List Position

Get all empty positions in a grid, sorted from lowest to highest.

values : Grid a -> List a

Get all of the values in a grid, in the order of their keys.

toList : Grid a -> List ( Position, a )

Convert a grid into an association list of position-value pairs, sorted by the position.

{ rows : Int
, columns : Int
}
-> List ( Position, a )
-> Grid a

Convert an association list into a grid.

Dict

toDict : Grid a -> Dict Position a

Convert a grid into an associated dictionary

{ rows : Int
, columns : Int
}
-> Dict Position a
-> Grid a

Convert an dictionary to a grid

Transform

map : (Position -> Maybe a -> Maybe b) -> Grid a -> Grid b

Apply a function to all positions in a grid.

foldl : (Position -> Maybe v -> b -> b) -> b -> Grid v -> b

Fold over all positions in a grid, row by row, from top down.

foldr : (Position -> Maybe v -> b -> b) -> b -> Grid v -> b

Fold over all positions in a grid, row by row, from bottum up.

filter : (Position -> a -> Bool) -> Grid a -> Grid a

Keep only the values that pass the given test.

partition : (Position -> a -> Bool) -> Grid a -> ( Grid a, Grid a )

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 : (Position -> a -> Bool) -> Grid a -> Maybe ( Position, a )

Find the first value that passes a given test.

Combine

union : Grid a -> Grid a -> Grid a

Combine two grids. If there is a collision, preference is given to the first grid.

intersect : Grid a -> Grid a -> Grid a

Keep a value when its position appears in the second grid. Preference is given to values in the first grid.

diff : Grid a -> Grid a -> Grid a

Keep a value when its position does not appear in the second grid.