JoshuaHall / elm-2d-array / Array2D

Provides an ergonomic and fast way to use 2 dimensional arrays in Elm.

2D Arrays


type Array2D a

Type representing a 2 dimensional array.

Creation

fromRows : Array (Array a) -> Maybe (Array2D a)

Attempts to create a Array2D from an Array of Arrays representing rows. Returns Nothing when the rows' lengths don't match.

fromColumns : Array (Array a) -> Maybe (Array2D a)

Attempts to create a Array2D from an Array of Arrays representing columns. Returns Nothing when the columns' lengths don't match.

fromRowMajor : Basics.Int -> Basics.Int -> Array a -> Maybe (Array2D a)

Attempts to create a Array2D from an Array of elements and the rows and columns amounts. Orders the elements in row major order. The array has to be the same length as the product of the provided rows and columns.

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

Creates an Array2D with the provided rows and columns. Each element is created using the provided function which gives the index (row and column).

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

Creates a Array2D with the provided rows and columns and fills it with the provided element.

Query

rows : Array2D a -> Basics.Int

Gets the number of rows the Array2D holds.

columns : Array2D a -> Basics.Int

Gets the number of columns the Array2D holds.

length : Array2D a -> Basics.Int

Gets the total number of elements the Array2D holds.

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

Gets an element from the Array2D at the provided row and column. Returns Nothing when the index is out of bounds.

Manipulation

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

Sets an element in the Array2D at the provided row and column. Returns the Array2D unchanged if the index is out of bounds.

update : Basics.Int -> Basics.Int -> (a -> a) -> Array2D a -> Array2D a

Updates an element in the Array2D with a function at the provided row and column. Returns the Array2D unchanged if the index is out of bounds.

Transform

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

Applies a function to every element in the Array2D.

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

Applies a function to every element in the Array2D. The index (row and column) are provided to the mapping function.

Arrays

toFlatArrayRowMajor : Array2D a -> Array a

Flattens the Array2D in row major order.

Randomness

generator : Random.Generator a -> Basics.Int -> Basics.Int -> Random.Generator (Array2D a)

Generates a Array2D where each value is randomly generated.