Orasund/elm-game-essentials/2.0.0

NOTE: the latest version is 3.0.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
= OutOfBounds
| NotSuccessful

Possible Errors.

  • OutOfBounds - the position is not valid
  • NotSuccessful - the function as a problem fulfilling the task. (Maybe the position is occupied?)
ignoringErrors : (Grid a -> Result Error (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

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

Create a grid

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

Create an empty grid

insert : ( Int, Int ) -> 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.

( Int, Int )
-> (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 : ( Int, Int ) -> 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 : ( Int, Int ) -> Grid a -> Result Error Bool

Determine if a position is empty. Returns an OutOfBounds Error if the position is invalid.

get : ( Int, Int ) -> Grid a -> Result Error (Maybe a)

Get the value associated with a position. If the position is empty, return Nothing. Returns an OutOfBounds Error if the position is invalid.

isValid : ( Int, Int ) -> 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 ( Int, Int )

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

emptyPositions : Grid a -> List ( Int, Int )

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 ( ( Int, Int ), a )

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

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

Convert an association list into a grid.

Dict

toDict : Grid a -> Dict ( Int, Int ) a

Convert a grid into an associated dictionary

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

Convert an dictionary to a grid

Transform

map : (( Int, Int ) -> Maybe a -> Maybe b) -> Grid a -> Grid b

Apply a function to all positions in a grid.

foldl : (( Int, Int ) -> Maybe v -> b -> b) -> b -> Grid v -> b

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

foldr : (( Int, Int ) -> Maybe v -> b -> b) -> b -> Grid v -> b

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

filter : (( Int, Int ) -> a -> Bool) -> Grid a -> Grid a

Keep only the values that pass the given test.

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