Array2d
Functions for working with 2-dimensional arrays (arrays of arrays).
Follows Array API semantics as closely as possible.
Alias for an array of arrays.
Initialize a 2-dimensional array with the given width and height.
The values are populated by calling your function with the current x
and y
coordinates for that element:
initialize
{ width = 2, height = 3 }
(\x y -> { x = x, y = y })
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} ]
]
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
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 ]
]
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 } ]
]
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
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
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 ]
, []
]
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
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
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
Find the first value that passes the test.
findFirst (\n -> n > 0) [ [ -1, 0 ], [ 1, 2 ] ] == Just 1
Find the last value that passes the test.
findLast (\n -> n > 0) [ [ -1, 0 ], [ 1, 2 ] ] == Just 2
Figure out whether a 2-dimensional array contains a value.
member 9 [ [ 1, 2 ], [ 3, 4 ] ] == False
member 4 [ [ 1, 2 ], [ 3, 4 ] ] == True
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
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
Find the minimum element in a non-empty 2-dimensional array.
minimum [[ 3, 2 ], [ 1 ]] == Just 1
minimum [[]] == Nothing
minimum [] == Nothing
Find the maximum element in a non-empty 2-dimensional array.
minimum [[ 3, 2 ], [ 1 ]] == Just 3
minimum [[]] == Nothing
minimum [] == Nothing
Modify
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 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]]