Array2d

Functions for working with 2-dimensional arrays (arrays of arrays).

Follows Array API semantics as closely as possible.

type alias Array2d a = Array (Array a)

Alias for an array of arrays.

initialize :
{ width : Int, height : Int } -> ({ x : Int, y : Int } -> a) -> Array2d a

Initialize a 2-dimensional array with the given width and height.

The values are populated by calling your function with the { x, y } coordinates for that element:

initialize { width = 2, height = 3 } identity

results in

[ [ {x=0, y=0}, {x=1, y=0} ]
, [ {x=0, y=1}, {x=1, y=1} ]
, [ {x=0, y=2}, {x=1, y=2} ]
]
repeat : { width : Int, height : Int } -> a -> Array2d a

Create a 2-dimensional array with the given width and height, filled with the given value:

repeat { width = 3, height = 2 } 'a'

results in:

[ [ 'a', 'a', 'a' ]
, [ 'a', 'a', 'a' ] 
]

Transform

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

Apply a function on every element in a 2-dimensional array.

map negate
    [ [ 1, 2, 3 ]
    , [ 4, 5, 6 ]
    , [ 7, 8, 9 ]
    ]

results in:

    [ [ -1, -2, -3 ]
    , [ -4, -5, -6 ]
    , [ -7, -8, -9 ]
    ]
indexedMap : ({ x : Int, y : Int } -> a -> b) -> Array2d a -> Array2d b

Same as map but the function also receives the x and y coordinates.

indexedMap (\{ x, y } val -> { x = x, y = y, val = val + 1 })
    [ [ 1, 2 ]
    , [ 3, 4 ]
    ]

results in:

    [ [ { x = 0, y = 0, val = 2 }, { x = 1, y = 0, val = 3 } ]
    , [ { x = 0, y = 1, val = 4 }, { x = 1, y = 1, val = 5 } ]
    ]
foldl : (a -> b -> b) -> b -> Array2d a -> b

Reduce the arrays starting from the left of each row, top down.

foldl (+) 0
    [ [ 1, 2, 3 ]
    , [ 4, 5, 6 ]
    ]

is like saying:

0
    |> (+) 1
    |> (+) 2
    |> (+) 3
    |> (+) 4
    |> (+) 5
    |> (+) 6

resulting in:

21
foldr : (a -> b -> b) -> b -> Array2d a -> b

Reduce the arrays starting from the right of each row, bottom up. The same as foldl, but the order is reversed.

foldr (-) 0
    [ [ 1, 2 ]
    , [ 3, 4 ]
    ]

is like saying:

0
    |> (-) 4  -- 4 - 0 = 4
    |> (-) 3  -- 3 - 4 = -1
    |> (-) 2  -- 2 - -1 = 3
    |> (-) 1  -- 1 - 3 = -2

resulting in:

-2
filter : (a -> Bool) -> Array2d a -> Array2d a

Keep values that pass the test. Empty rows will be left in place.

filter (\n -> n < 5) 
    [ [ 1, 2, 3 ] 
    , [ 4, 5, 6 ]
    , [ 7, 8, 9 ]
    ]

results in:

[ [ 1, 2, 3 ]
, [ 4 ]
, []
]
filterMap : (a -> Maybe b) -> Array2d a -> Array2d b

Filter out unwanted results of a map operation. Empty rows will be left in place

filterMap String.toInt
    [ [ "3", "not a number", "-5" ]
    , [ "also not a number " ]
    ]

results in:

[ [ 3, -5 ]
, []
]

Query

get : { x : Int, y : Int } -> Array2d a -> Maybe a

Retrieve the element at the given x y coordinate. Returns Nothing if either coordinate is out of bounds.

get {x = 0, y = 1} [[1, 2], [3, 4]] == Just 3
get {x = 2, y = 1} [[1, 2], [3, 4]] == Nothing
size : Array2d a -> Int

Return the total size of the array of arrays.

This is determined by adding the lengths of all the rows.

size [ [ 1, 2 ], [ 3, 4 ] ] == 4
findFirst : (a -> Bool) -> Array2d a -> Maybe a

Find the first value that passes the test.

findFirst (\n -> n > 0) [ [ -1, 0 ], [ 1, 2 ] ] == Just 1
findLast : (a -> Bool) -> Array2d a -> Maybe a

Find the last value that passes the test.

findLast (\n -> n > 0) [ [ -1, 0 ], [ 1, 2 ] ] == Just 2
member : a -> Array2d a -> Bool

Figure out whether a 2-dimensional array contains a value.

member 9 [ [ 1, 2 ], [ 3, 4 ] ] == False
member 4 [ [ 1, 2 ], [ 3, 4 ] ] == True
any : (a -> Bool) -> Array2d a -> Bool

Determine if any elements pass the test.

any isEven [ [ 1, 2 ], [ 3, 4 ] ] == True
any isEven [ [ 1, 3 ], [ 3, 5 ] ] == False
any isEven [[]] == False
any isEven [] == False
all : (a -> Bool) -> Array2d a -> Bool

Determine if all elements pass the test.

any isEven [ [ 1, 2 ], [ 3, 4 ] ] == False
any isEven [ [ 2, 4 ], [ 6, 8 ] ] == True
any isEven [[]] == True
any isEven [] == True
minimum : Array2d comparable -> Maybe comparable

Find the minimum element in a non-empty 2-dimensional array.

minimum [[ 3, 2 ], [ 1 ]] == Just 1
minimum [[]] == Nothing
minimum [] == Nothing
maximum : Array2d comparable -> Maybe comparable

Find the maximum element in a non-empty 2-dimensional array.

minimum [[ 3, 2 ], [ 1 ]] == Just 3
minimum [[]] == Nothing
minimum [] == Nothing

Modify

set : { x : Int, y : Int } -> a -> Array2d a -> Array2d a

Returns the 2-d array with a value replaced at the given coordinate, or unmodified if either coordinate is out of bounds.

set { x = 1, y = 0 } 9
    [ [ 0, 0, 0 ]
    , [ 0, 0, 0 ]
    ]

results in

[ [ 0, 9, 0 ]
, [ 0, 0, 0 ]
]

and

set { x = 2, y = 1 } 0 [[1]] == [[1]]
update : { x : Int, y : Int } -> (a -> a) -> Array2d a -> Array2d a

Update a value at the given coordinates using a function. If the x or y position is out of bounds, nothing happens.

update { x = 1, y = 0 } (\n -> n + 1) [[1, 2], [3, 4]] == [[1, 3], [3, 4]]
update { x = 1, y = 9 } (\n -> n + 1) [[1, 2], [3, 4]] == [[1, 2], [3, 4]]