tortus/elm-array-2d/1.0.0

NOTE: this package is not compatible with Elm 0.19.1

Array2D

Implements 2D array using nested Arrays. Useful for implementing data grids, as it specifically provides row and column operations.

Cells are accessed by row, then column. Otherwise, it works very similarly to the Array class. The documentation examples usually omit converting Lists to Arrays for brevity.

Array2D's can be constructed from an Array or List of rows, where each row is an Array or List of cells. Behavior if the nested arrays happen to be jagged is currently undefined / handled poorly, so don't do this!

Array2D.fromList [["Row 1-Col 1", "Row 1-Col 2"], ["Row 2-Col 1", "Row 2-Col 2"]]
type alias Array2D a =
{ data : Array (Array a) }

Base Array2D type

Initialization

empty : Array2D a

Create an empty Array2D

fromArray : Array (Array a) -> Array2D a

Create an Array2D from an Array of Arrays.

let
  row1 = Array.fromList [1, 2]
  row2 = Array.fromList [2, 3]
in
  fromArray (Array.fromList [row1, row2])
fromList : List (List a) -> Array2D a

Create an Array2D from a List of Lists.

fromList [[1, 2, 3], [4, 5, 6]]
repeat : Int -> Int -> a -> Array2D a

Create a 2D of a given size, filled with a default element. Similar to Array.repeat

repeat 2 3 0 == [[0, 0, 0], [0, 0, 0]]

Getting info

rows : Array2D a -> Int

Get the number of rows in an Array2D

rows [[1, 2, 3], [4, 5, 6]] == 2
columns : Array2D a -> Int

Get the number of columns in an Array2D

columns [[1, 2, 3], [4, 5, 6]] == 3
isEmpty : Array2D a -> Bool

Check if an Array2D is empty.

isEmpty [] == True

Fetching/updating individual cells

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

Get a cell.

get 1 1 [[1, 2], [3, 4]] == Just 4
set : Int -> Int -> a -> Array2D a -> Array2D a

Update a cell, returning the changed Array2D.

set 0 0 -100 [[1, 2], [3, 4]] == [[-100, 2], [3, 4]]

Adding/removing rows

getRow : Int -> Array2D a -> Maybe (Array a)

Get an individual row

getRow 1 [[1, 2], [3, 4]] == Just [3, 4]
appendRow : Array a -> Array2D a -> Array2D a

Append a row

appendRow [3, 4] [[1, 2]] == [[1, 2], [3, 4]]
deleteRow : Int -> Array2D a -> Array2D a

Delete a row

Adding/removing columns

getColumn : Int -> Array2D a -> Array (Maybe a)

get column-th cell of each row as an Array

getColumn 1 [[1, 2], [3, 4]] == [Just 2, Just 4]
deleteColumn : Int -> Array2D a -> Array2D a

Delete a column

Mapping cell data

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

2D version of Array.map.

map (\cell -> toString cell) [[1, 2], [3, 4]] == [["1", "2"], ["3", "4"]]
indexedMap : (Int -> Int -> a -> b) -> Array2D a -> Array2D b

2D version of Array.indexedMap. First two arguments of map function are the row and column.

indexedMap (\row column cell -> toString row) [[1, 2], [3, 4]] == [["0", "0"], ["1", "1"]]