Orasund/pixelengine/5.0.3

NOTE: the latest version is 6.2.0

Grid

DEPRECATED! Got moved to its separate package.

A Grid is a dictionary that has a size constraint. Here is an example where such a grid is used: Snake Example.

Grids

type Grid a

A grid with a fixes amount of columns and rows.

It will wrap the borders (apply ModBy), making every position valid.

dimensions : { columns:Int , rows:Int }
dimensions =
    { columns=42
    , rows=3
    }


grid : Grid a
grid =
    empty dimensions


grid |> get ( -1, 0 )
--> grid |> get ( (dimensions |> .columns) - 1, 0 )

If instead you want to have hard border around your grid, use Grid.Bordered instead.

Build

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

Create a grid

dimensions : { columns:Int , rows:Int }
dimensions =
    { columns=42
    , rows=3
    }


grid : Grid a
grid =
    empty dimensions


fill (always <| Just ()) dimensions |> emptyPositions
--> []
{ rows : Int
, columns : Int
}
-> Grid a

Create an empty grid

dimensions : { columns:Int , rows:Int }
dimensions =
    { columns=42
    , rows=3
    }


empty dimensions
--> fill (always Nothing ) dimensions
insert : Position -> a -> Grid a -> Grid a

Insert a value at a position in a grid. Replaces value when there is a collision.

dimensions : { columns:Int , rows:Int }
dimensions =
    { columns=42
    , rows=3
    }


grid : Grid a
grid =
    empty dimensions


grid |> insert (2,2) 42 |> get (2,2)
--> Just 42
update : Position -> (Maybe a -> Maybe a) -> Grid a -> Grid a

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

dimensions : { columns:Int , rows:Int }
dimensions =
    { columns=42
    , rows=3
    }


grid : Grid a
grid =
    empty dimensions


grid |> update (2,2) (always <| Just 42)
--> grid |> insert (2,2) 42
remove : Position -> Grid a -> Grid a

Remove a vlaue from a grid. If the position is empty, no changes are made.

dimensions : { columns:Int , rows:Int }
dimensions =
    { columns=42
    , rows=3
    }


grid : Grid a
grid =
    empty dimensions


grid |> insert (2,2) 42 |> get (2,2) --> Just 42
grid |> insert (2,2) 42 |> remove (2,2) |> get (2,2)
--> Nothing

Query

isEmpty : Grid a -> Bool

Determine if a grid is empty.

dimensions : { columns:Int , rows:Int }
dimensions =
    { columns=42
    , rows=3
    }


grid : Grid a
grid =
    empty dimensions


grid |> isEmpty --> True
grid |> insert (2,2) 42 |> isEmpty --> False
member : Position -> Grid a -> Bool

Determine if a position is empty.

dimensions : { columns:Int , rows:Int }
dimensions =
    { columns=42
    , rows=3
    }


grid : Grid a
grid =
    empty dimensions


grid |> insert (2,2) 42 |> member (2,2)
--> True
get : Position -> Grid a -> Maybe a

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

dimensions : { columns:Int , rows:Int }
dimensions =
    { columns=42
    , rows=3
    }


grid : Grid a
grid =
    empty dimensions


grid |> insert (2,2) 42 |> get (2,2)
--> Just 42
size : Grid a -> Int

Determine the number of values in the grid.

dimensions : { columns:Int , rows:Int }
dimensions =
    { columns=42
    , rows=3
    }


grid : Grid a
grid =
    empty dimensions


grid |> insert (2,2) 42 |> size
--> 1
Grid a
-> { columns : Int
, rows : Int
}

Return the dimensions of the grid.

dim : { columns:Int , rows:Int }
dim =
    { columns=42
    , rows=3
    }


grid : Grid a
grid =
    empty dim


grid |> dimensions
--> dim

List

positions : Grid a -> List Position

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

dimensions : { columns:Int , rows:Int }
dimensions =
    { columns=42
    , rows=3
    }


grid : Grid a
grid =
    empty dimensions


grid |> insert (2,2) 42 |> positions
--> [(2,2)]
emptyPositions : Grid a -> List Position

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

dimensions : { columns:Int , rows:Int }
dimensions =
    { columns=42
    , rows=3
    }


grid : Grid Int
grid =
    fill
        (always <| Just <| 42)
        dimensions


grid |> remove (2,2) |> emptyPositions
--> [(2,2)]
values : Grid a -> List a

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

dimensions : { columns:Int , rows:Int }
dimensions =
    { columns=42
    , rows=3
    }


grid : Grid a
grid =
    empty dimensions


grid |> insert (2,2) 42 |> values
--> [42]
toList : Grid a -> List ( Position, a )

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

dimensions : { columns:Int , rows:Int }
dimensions =
    { columns=42
    , rows=3
    }


grid : Grid Int
grid =
    empty dimensions


grid |> insert (2,2) 42 |> toList
--> [( (2,2), 42 )]
{ rows : Int
, columns : Int
}
-> List ( Position, a )
-> Grid a

Convert an association list into a grid.

dimensions : { columns:Int , rows:Int }
dimensions =
    { columns=42
    , rows=3
    }


grid : Grid Int
grid =
    empty dimensions


[((2,2),42),((2,1),20)] |> fromList dimensions
--> grid |> insert (2,2) 42 |> insert (2,1) 20

Dict

toDict : Grid a -> Dict Position a

Convert a grid into an associated dictionary

dimensions : { columns:Int , rows:Int }
dimensions =
    { columns=42
    , rows=3
    }


grid : Grid Int
grid =
    empty dimensions
        |> insert (2,2) 42


grid |> toDict |> fromDict dimensions |> get (2,2)
--> Just 42
{ rows : Int
, columns : Int
}
-> Dict Position a
-> Grid a

Convert an dictionary to a grid

dimensions : { columns:Int , rows:Int }
dimensions =
    { columns=42
    , rows=3
    }


grid : Grid Int
grid =
    empty dimensions


grid |> toDict |> fromDict dimensions |> get (2,2)
--> Nothing

Transform

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

Apply a function to all positions in a grid.

dimensions : { columns:Int , rows:Int }
dimensions =
    { columns=42
    , rows=3
    }


empty dimensions |> map (\_ _ -> Just 42)
--> fill (always <| Just 42) dimensions
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.

dimensions : { columns:Int , rows:Int }
dimensions =
    { columns=42
    , rows=3
    }


grid : Grid Int
grid =
    empty dimensions
        |> insert (2,4) 2
        |> insert (2,3) 42


grid |> filter (\_ -> (==) 42) |> values
--> [42]
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.