perty / matrix / Matrix

Two-dimensional matrix backed by Array from the Elm core, the fast immutable array implementation.

Definition


type alias Matrix a =
Array (Array a)

Representation of immutable, two dimensional matrix. You can create a matrix of integers (Matrix Int) or strings (Matrix String) or any other type of value you can dream up.

Creation

empty : Matrix a

Return an empty matrix.

size empty == ( 0, 0 )

initialize : Basics.Int -> Basics.Int -> (Basics.Int -> Basics.Int -> a) -> Matrix a

Initialize a matrix, given desired size and a function for the value of a cell, given its x and y.

Matrix.initialize 100 100 (\x y -> String.fromInt x ++ "," ++ String.fromInt y)

repeat : Basics.Int -> Basics.Int -> a -> Matrix a

Initialize a matrix, given desired size and the value for every cell.

Matrix.repeat 100 100 InitialValue

Query

size : Matrix a -> ( Basics.Int, Basics.Int )

Return the size of a matrix in the form of a tuple, (sizeX, sizeY).

get : Matrix a -> Basics.Int -> Basics.Int -> Maybe a

Maybe get the value of the cell at (x,y).

getXs : Matrix a -> Basics.Int -> Array a

Get all values along a given x as an array. If x is out of bounds, return an empty array.

getYs : Matrix a -> Basics.Int -> Array a

Get all values along a given y as an array. If y is out of bounds, return an empty array.

neighbours : Matrix a -> Basics.Int -> Basics.Int -> Array (Maybe a)

Return an array of possible neighbour cells for a given cell. It is an array of Maybe, compare the get function that is a single Maybe.

Manipulate

set : Matrix a -> Basics.Int -> Basics.Int -> a -> Matrix a

Set the cell at (x,y) to a new value. If the (x,y) is out of bounds, silently do nothing,

Transform

map : (a -> b) -> Matrix a -> Matrix b

Apply a function on every element in a matrix.

Matrix.map (\\n -> n \* 2) [ [ 0, 0, 0 ], [ 0, 1, 2 ] ] => [ [ 0, 0, 0 ], [ 0, 2, 4 ] ]

indexedMap : (Basics.Int -> Basics.Int -> a -> b) -> Matrix a -> Matrix b

Apply a function on every element with its x and y as first arguments.

Matrix.indexedMap (\\x y \_ -> (String.fromInt x + "," + String.fromInt y)(Matrix.repeat 2 3 "")
=> [[ "0,0", "0,1", "0,2" ],[ "1,0", "1,1", "1,2" ] ]